Shell scripts are awesome

DeChamp - Nov 13 '19 - - Dev Community

(big thanks for Ben Sinclair's comment to help correct some things.)

I love using command line, it something about it's simplicity and power.

I just wanted to give a quick idea out there, to help speed up your day. I also hope you'll add any of your fun and helpful tools!

So here are my favs.

Aliases are super useful and I have a bunch. If you want them, message me. If i get enough response I'll make a post about it. But this is more focussed on functions and module combination to do some neat stuff.

First off, this post started because just now i was like "why am i retyping this every time!" and I wrote a quick function in 10 seconds, and realized I should share this!

So at work we do git forks, and then we branch per story.

I personally call the origin/master "upstream" and my fork of it by name name.

Each time I start a new story I have to run the following commands.

git fetch upstream
git checkout -b STORY_NUMBER_WITH_BRIED_DESC upstream/master
git pull upstream master // repetitive but for some reason fetch doesn't always update for me

So I type that literally every time I start a new story. It's a lot to type especially when you have more than 1 services that will be involved.

So this little function in my ~/.zshrc file (you may be using .bash_profile) make it easier!

newStory() {
    git fetch upstream && git checkout -b $1 upstream/master && git pull upstream master;
}
Enter fullscreen mode Exit fullscreen mode

to use it, just make sure to source you new changes source ~/.zshrc and now I just type

newStory B-23168_automated_testing
Enter fullscreen mode Exit fullscreen mode

There are many other one off shell script functions I use. I have a hard time remembering how to spell words. So I have a spl command which behind the scenes will run the aspell module. Which you can install via brew.

spl () {
    aspell -a <<< "$1"
}
Enter fullscreen mode Exit fullscreen mode

Now I run my new command and it'll give me back what it thinks I mean. The first 1 or 2 words are almost always the one you meant.

spl polymorfus

> & polymorfus 3 0: polymorphous, polymers, polymer's
Enter fullscreen mode Exit fullscreen mode

Maybe you wanna create your own function that call a custom script to do more than you know how to do in bash. Maybe PHP is your best language and you suck as bash other than the basics?

No problem!

Add your php shell script. Simple as using #!/path/to/php and then your php script. You can get your paths by using which php

~/.settings/snippets/urldecode (no extension)

#!/usr/bin/php
<?php

echo urldecode($argv[1]);
?>
Enter fullscreen mode Exit fullscreen mode

now add your snippets folder export to the end of the ~/.zshrc file. This will make any script in this folder available to the command line! (thanks Ben for the better solution)

export PATH=$HOME/.settings/snippets:$PATH
Enter fullscreen mode Exit fullscreen mode

and boom💥 you have a new script of your own making!

urldecode this%20is%20my%20string%21%20

> this is my string! %
Enter fullscreen mode Exit fullscreen mode

So as you can see, you can do a lot with bash. It's awesome! I've dumped a few more below that you might wanna use. If you see it using another function name, you will probably need to brew install or npm install it.

# take every file and move it to the current folder
#
# with in parent folder you want, run flattenSubfolders
flattenSubfolders () {
    find . -type f -exec mv -n -- {} . \;
}

# used after you use flattenSubfolders to remove empty subfolders
#
# with in parent folder you want, run removeEmptySubdirs
removeEmptySubdirs () {
    find . -depth -mindepth 1 -type d -empty -exec rmdir {} \;
}

# will open chrome and turn your README.md in to a nice web view, requires https://github.com/joeyespo/grip 
#
# rmd ./README.md
rmd () {
    grip -b $1 &
    TASK_PID=$!
    sleep 10
    kill $TASK_PID
}

# video helpers 
#
# genWebMp4 ./movie.ext ./newmovie.mp4
genWebMp4 () {
    ffmpeg -i $1 -vcodec h264 -acodec aac -strict -2 $2
}

#
# genWebm ./movie.ext ./newmovie.webm
genWebm () {
    ffmpeg -i $1 -c:v libvpx-vp9 -b:v 2M -pass 1 -c:a libopus -f webm /dev/null && \
        ffmpeg -i $1 -c:v libvpx-vp9 -b:v 2M -pass 2 -c:a libopus $2
}

#
# wavToMp3 ./file
wavToMp3() {
    ffmpeg -i $1.wav -codec:a libmp3lame -qscale:a 2 $1.mp3
}

#
# with in folder of all mp3s, run mp3ToOgg
mp3ToOgg() {
    for file in *.mp3
        do ffmpeg -i "${file}" "${file/%mp3/ogg}"
    done
}

#
# genVideoThumb ./video.ext ./thumbnail.ext
# you can use jpg and png for thumbnail ext
genVideoThumb () {
    ffmpeg -ss 00:00:01  -i $1 -vframes 1 -q:v 2 $2
}

#
# spl word
spl () {
    aspell -a <<< "$1"
}

#
# gitCleanBranches
gitCleanBranches() {
    git branch --merged | egrep -v "(^\*|master|develop)" | xargs git branch -d
}
Enter fullscreen mode Exit fullscreen mode

Hope you liked it. See an issue? Let me know.

Please add yours in the comments!


Varymade LLC.

Current projects are https://charactergenerator4000.com and https://coder.exchange. Please check them out and let us know your thoughts.

--DeChamp

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player