some training and useful files for shell scripting
create the file with:
touch hello_world.sh
decide the shell with:
echo "#! /bin/bash" >> hello_world.sh
echo "echo 'hello world'" >> hello_world.sh
then we have to change the permissions with:
NOTE: it's not good to add a permission to all users you may use 'chmod u+x' to change for the user only
chmod +x ./hello_world.sh
then call the file in the terminal with:
./hello_world.sh
pipelines are where the input of a state is the output of the previous one and so on.
you can use the pipeline as follows cmd1 | cmd2
so imagin we have a file and want to get the unique values of it we can do this
sort [file name] | uniq
Some commands, such as tr, only accept "standard input" as input (not strings or filenames)
we use pipline for them as follows:
echo "Linux and shell scripting are awesome!" | tr "aeiou" "_"
OUTPUT: L_n_x _nd sh_ll scr_pt_ng r w_s_m!
It's also pretty useful filtering text or o/p of curl commands with grep as follows:
curl -s --location --request GET https://www.example.com | grep -oE ""price":\s*[0-9]?.[0-9]"
# for comments
; for ending one command to start another in the same line
* indicates any number of charecters as in 'ls /bin/bas*'
? indicates 1 character as in 'ls /bin/?ash'
\ this is used before metacharactes to interpret them as text
"" must use \ before metacharacters while '' interpret all text as text
> redirect output to file and create one if not existed and override if it exists
>> append output to file
2> catchs error and writes them to a file
2>> catchs error and apend it to a file
< to pass file content to a standard i/p command
&(command) or `command` is used for replacing command with its o/p as in $ variable=$(pwd)
./random_file arg1 arg2 passing arguments to shell script
; runs commands sequentially
& runs commands parrelel
cron allows u to do that
crond is the cron deamon which checks 'crontab files' every minute and submits job to cron
crontab is a table contains jobs and scedule data
crontab -e opens the editor
U add commands describing jobs as follows:
m h dom mon dow command
all 5 position must have numeric or * for any
dow 0:sunday and so on
crontab -l gets the jobs and scedules