Automate your workflow with shell script
Published on: 2021-11-22
Executable files
To make your script executable is necessary to set shebangs and add permissions to file.
Files can be used for three main mode:
r- read modew- write modex- executable mode
If we want to run our script by clicking on it or by command ./script_name we need
to set up executable mode to this file. Otherwise we are not able to run this script.
Check what modes are set up on your files run command ls with option -l. It
shows you file in your folder includes information about modes something like
drwxr-xr-x.
ls -l
the schmema drwxr-xr-x means from left to right:
first char which is equal to
dmeans this file isdirectory, when you just slash-on this position then means the file is normal file.next three chars
rwxmeans setup permissions foruseroru. Permissions are always setup in same order it means firs is alwaysr, second iswand last one isx. When permission is not setup there is slash-instead of one of permissionrwx.next three char
r-xis absolutely same as foruserabove but this set of chars is meant togrouporgadn we can see that forgroupkind of users theres is no any write permissions allowed.last three chars are permissions for
otherorouseroruwhat can do the usergrouporgwhat can do the users of same groupotherorowhat can do the anybody else
If we want to change permission we have to use command chmod.
Example we want to add read and executable permission to file with name hello.sh and just
for user
chmod u+rx hello.sh
We can also delete permissions if we want for example for other. We remove
read and write permission.
chmod o-rw hello.sh
As you can mention when we want to add permission we use mark +, otherwise
when we want to remove we use mark -.
schema is:
chmod who(u, g, o) add/remove(+, -) permissions(r, w, x) filename
Shebangs
Shebang is used when we want to make some script executable. Shebang has to be write at the beginning of the file.
Shebangs are described nicely on wiki wiki shebangs.
Generally shebang schema looks like:
#!interpreter [optional-arg]
you have to specified path to intepreter which will run the script. So you dont have to write to CLI because you will tell to cli at the beginnig of the file.
Sometimes shell will evaluate automatically for example for bash, zsh, etc. scripts.
At the end of day you wont be write python3 filename.py but only ./filename
for shebang #!/usr/bin/env python3
Arguments from CLI
If you want to take some arguments from CLI, these arguments are saved in variables $0, $1, $2
- $0 - scriptname
 - $1 - first argument
 - $2 - second argument
 - $n - n-argument
 
you can specify that first argument is required as:
if [ ! "$1" ]
then
    echo "Missing first argument\n"
    echo "Usage: ./script.sh 'first argument'\n"
    exit 1
fi
echo "Your argument is: $1"
Make your script for automation
If you use very often some same commands in CLI you can write these commands to file as shell script for example some script.sh.
So you will save time to write all commands repetitive again and again. and you just run script shell for one time.
To correctly run your script you have to set up shebang and give execute permissions
to that file.
the script should be somethin like this
script.sh
#!/bin/sh
if [ ! "$1" ]
then
    echo "Missing commit message\n"
    echo "Usage: ./generate_content.sh 'commit message to github'\n"
    exit 1
fi
sudo apt update && sudo apt upgrade
git add .
git commit $1
git push origin master
echo "scrript completed"