User Tools

Site Tools


scripts-and-functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
scripts-and-functions [2019/10/08 00:26]
Miroslav Bernát
scripts-and-functions [2020/02/16 22:03] (current)
Miroslav Bernát
Line 8: Line 8:
 | **case** <​string>​ **in** <​pattern>​(%%|%%<​pattern>​**)** <​list>​**;;​ esac** | executes a command provided the string matches the pattern; the string represents any variable, pattern „*“ specifies an action if both the values do not match (similar to command „else“),​ the list of patterns is closed with „)“ and the group of commands ends up with „;;“\\ ''#​!/​bin/​bash''​\\ ''#​ The script creates or deletes a file according to the action.''​\\ ''​echo -n 'Enter an action and file name: '''​\\ ''​read ACTION FILE''​\\ ''​case $ACTION in''​\\ <​tab>''​remove|delete)<​tab>​rm $FILE;;''​\\ <​tab>''​make|create) <tab> touch $FILE''​\\ <​tab><​tab><​tab><​tab><​tab>​ <tab> <tab> <​tab>''​chmod 777 $FILE;;''​\\ <​tab>''​*) <tab> <​tab><​tab>​echo '​Unknown operation';;''​\\ ''​esac''​ | | **case** <​string>​ **in** <​pattern>​(%%|%%<​pattern>​**)** <​list>​**;;​ esac** | executes a command provided the string matches the pattern; the string represents any variable, pattern „*“ specifies an action if both the values do not match (similar to command „else“),​ the list of patterns is closed with „)“ and the group of commands ends up with „;;“\\ ''#​!/​bin/​bash''​\\ ''#​ The script creates or deletes a file according to the action.''​\\ ''​echo -n 'Enter an action and file name: '''​\\ ''​read ACTION FILE''​\\ ''​case $ACTION in''​\\ <​tab>''​remove|delete)<​tab>​rm $FILE;;''​\\ <​tab>''​make|create) <tab> touch $FILE''​\\ <​tab><​tab><​tab><​tab><​tab>​ <tab> <tab> <​tab>''​chmod 777 $FILE;;''​\\ <​tab>''​*) <tab> <​tab><​tab>​echo '​Unknown operation';;''​\\ ''​esac''​ |
 | **read** <​word>​ | reads the line from STDIN and splits it into words which are then assigned to particular variables for further processing, **-n** <​number_of_characters>​ reads a specified number of characters only, **-r** suppresses a special meaning of „\“ character, **-s** does not display particular characters\\ ''#​!/​bin/​bash''​\\ ''#​ The script asks the user to enter a user name and password and confirms it.''​\\ ''​echo -n "​Username:​ "; read username''​\\ ''​echo -n "​Password:​ "; read -s password''​\\ ''​echo " "''​\\ ''​echo "​Password for $username submitted."''​\\ \\ ''​$ cat test | { read a; read b; read c; echo "​$a/​$b/​$c";​}''​ | | **read** <​word>​ | reads the line from STDIN and splits it into words which are then assigned to particular variables for further processing, **-n** <​number_of_characters>​ reads a specified number of characters only, **-r** suppresses a special meaning of „\“ character, **-s** does not display particular characters\\ ''#​!/​bin/​bash''​\\ ''#​ The script asks the user to enter a user name and password and confirms it.''​\\ ''​echo -n "​Username:​ "; read username''​\\ ''​echo -n "​Password:​ "; read -s password''​\\ ''​echo " "''​\\ ''​echo "​Password for $username submitted."''​\\ \\ ''​$ cat test | { read a; read b; read c; echo "​$a/​$b/​$c";​}''​ |
-| **while** <​list1>​**;​ do** <​list2>​**;​ done** | executes repeatedly „list2“ until „list1“ returns a successful return code\\ ''#​!/​bin/​bash''​\\ ''#​ The script opens four terminal screens.''​\\ ''​I=0''​\\ ''​while [ $i -lt 4 ]; do xterm &''​\\ <​tab>''​i=$[$i+1];​ done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script creates a file every five minutes.''​\\ ''​while true; do''​\\ <​tab>''​touch pic-`date|awk '​{print $2,​$3,​$4,​$6}'​|sed 's/ /​_/​g'​`.jpg''​\\ <​tab>''​sleep 300; done''​ |+| **while** <​list1>​**;​ do** <​list2>​**;​ done** | executes repeatedly „list2“ until „list1“ returns a successful return code\\ ''#​!/​bin/​bash''​\\ ''#​ The script opens four terminal screens.''​\\ ''​i=0''​\\ ''​while [ $i -lt 4 ]; do xterm &''​\\ <​tab>''​i=$[$i+1];​ done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script creates a file every five minutes.''​\\ ''​while true; do''​\\ <​tab>''​touch pic-`date|awk '​{print $2,​$3,​$4,​$6}'​|sed 's/ /​_/​g'​`.jpg''​\\ <​tab>''​sleep 300; done''​ |
 | **until** <​list1>​**;​ do** <​list2>​**;​ done** | executes repeatedly „list2“ until „list1“ returns an unsuccessful return code\\ ''#​!/​bin/​bash''​\\ ''#​ The script prints numbers ​ 0–99.''​\\ ''​a=-1''​\\ ''​until [ $a -eq 99 ]; do''​\\ <​tab>''​a=$(expr $a + 1)''​\\ <​tab>''​echo $a; done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script copies files from home to web directory where it creates a new directory every\\ # hour and in case it is more than 90 % full it removes directories older than 30 days.''​\\ ''​while true; do''​\\ <​tab>''​DISKFUL=$(df -h $WEBDIR | grep -v File | \''​ \\ <​tab>''​awk '​{print $5 }' | cut -d "​%"​ -f1 -)''​\\ \\ <​tab>''​until [ $DISKFUL -ge "​90"​ ]; do''​\\ ​ <​tab><​tab>''​WEBDIR=/​var/​www/​webcam''​\\ <​tab><​tab>''​DATE=`date +%Y%m%d`''​\\ <​tab><​tab>''​HOUR=`date +%H`''​\\ <​tab><​tab>''​mkdir $WEBDIR/"​$DATE"''​\\ \\ <​tab><​tab><​tab>''​while [ $HOUR -ne "​00"​ ]; do''​\\ <​tab><​tab><​tab>''​PICDIR=/​home/​user/​pics''​\\ <​tab><​tab><​tab>''​DESTDIR=$WEBDIR/"​$DATE"/"​$HOUR"''​\\ <​tab><​tab><​tab>''​mkdir "​$DESTDIR"''​\\ <​tab><​tab><​tab>''​cp $PICDIR/​*.jpg "​$DESTDIR"/''​\\ <​tab><​tab><​tab>''​sleep 3600''​\\ <​tab><​tab><​tab>''​HOUR=`date +%H`; done''​\\ \\ <​tab>''​DISKFULL=$(df -h $WEBDIR | grep -v File | \''​\\ <​tab>''​awk '{ print $5 }' | cut -d "​%"​ -f1 -)''​\\ <​tab>''​done''​\\ \\ ''​TOREMOVE=$(find $WEBDIR -type d -mtime +30)''​\\ <​tab>''​for i in $TOREMOVE; do''​\\ <​tab>''​rm -rf "​$i";​ done''​\\ ''​done''​ | | **until** <​list1>​**;​ do** <​list2>​**;​ done** | executes repeatedly „list2“ until „list1“ returns an unsuccessful return code\\ ''#​!/​bin/​bash''​\\ ''#​ The script prints numbers ​ 0–99.''​\\ ''​a=-1''​\\ ''​until [ $a -eq 99 ]; do''​\\ <​tab>''​a=$(expr $a + 1)''​\\ <​tab>''​echo $a; done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script copies files from home to web directory where it creates a new directory every\\ # hour and in case it is more than 90 % full it removes directories older than 30 days.''​\\ ''​while true; do''​\\ <​tab>''​DISKFUL=$(df -h $WEBDIR | grep -v File | \''​ \\ <​tab>''​awk '​{print $5 }' | cut -d "​%"​ -f1 -)''​\\ \\ <​tab>''​until [ $DISKFUL -ge "​90"​ ]; do''​\\ ​ <​tab><​tab>''​WEBDIR=/​var/​www/​webcam''​\\ <​tab><​tab>''​DATE=`date +%Y%m%d`''​\\ <​tab><​tab>''​HOUR=`date +%H`''​\\ <​tab><​tab>''​mkdir $WEBDIR/"​$DATE"''​\\ \\ <​tab><​tab><​tab>''​while [ $HOUR -ne "​00"​ ]; do''​\\ <​tab><​tab><​tab>''​PICDIR=/​home/​user/​pics''​\\ <​tab><​tab><​tab>''​DESTDIR=$WEBDIR/"​$DATE"/"​$HOUR"''​\\ <​tab><​tab><​tab>''​mkdir "​$DESTDIR"''​\\ <​tab><​tab><​tab>''​cp $PICDIR/​*.jpg "​$DESTDIR"/''​\\ <​tab><​tab><​tab>''​sleep 3600''​\\ <​tab><​tab><​tab>''​HOUR=`date +%H`; done''​\\ \\ <​tab>''​DISKFULL=$(df -h $WEBDIR | grep -v File | \''​\\ <​tab>''​awk '{ print $5 }' | cut -d "​%"​ -f1 -)''​\\ <​tab>''​done''​\\ \\ ''​TOREMOVE=$(find $WEBDIR -type d -mtime +30)''​\\ <​tab>''​for i in $TOREMOVE; do''​\\ <​tab>''​rm -rf "​$i";​ done''​\\ ''​done''​ |
 | **for** <​variable>​ **in** <​string>​**;​** **do** <​list>​**;​ done** | assigns a variable one by one the value of all parameters of the script or a particular string and executes a list of commands\\ ''#​!/​bin/​bash''​\\ ''#​ The script creates users specified in "​users"​ file in the working directory,​\\ # adds them to "​admins"​ group and assigns "​password"​ as their password.''​\\ ''​for user in $(cat users); do''​\\ <​tab>''​useradd -g admins $user''​\\ <​tab>''​echo password | passwd --stdin $user''​\\ ''​done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script prints the contents of the working directory.''​\\ ''​for x in $(ls -F)''​\\ ''​do''​\\ <​tab>''​echo "​Directory `pwd` contains file/​directory:​ $x"''​\\ ''​done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script counts the number of items in the working directory.''​\\ ''​POC=0''​\\ ''​for name in *''​\\ <​tab>''​do''​\\ <​tab><​tab>''​POC=$%%(($POC+1))%%''​\\ <​tab>''​done''​\\ ''​echo "​Directory `pwd` contains $POC items."''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script converts the format of all .tif files to .jpg.''​\\ ''​for pic in *.tif; do''​\\ <​tab>''​convert "​$pic"​ "​$(echo "​$pic"​ | sed '​s/​\.tif/​.jpg/'​)"''​\\ ''​done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script checks which of the particular processes are running, prints their names\\ # and the number of lines they occupy and finally prints all running processes\\ # and the total number of lines.''​\\ ''​services="​http pmon lsnr sap"''​\\ ''​for service in $services''​\\ <​tab>''​do running_processes="​$(ps -ef | grep $service | grep -v grep)"''​\\ <​tab><​tab>''​if [ -n "​$running_processes"​ ]''​\\ <​tab><​tab><​tab>''​then echo''​\\ <​tab><​tab><​tab>''​echo $service:''​\\ <​tab><​tab><​tab>''​echo''​\\ <​tab><​tab><​tab>''​echo "​$running_processes"''​\\ <​tab><​tab><​tab>''​echo''​\\ <​tab><​tab><​tab>''​echo "​lines:​ $(echo "​$running_processes"​ | wc -l)"''​\\ <​tab><​tab><​tab>''​echo''​\\ <​tab><​tab><​tab>''​echo "​=========="''​\\ <​tab><​tab>''​fi''​\\ <​tab>''​done''​\\ ''​echo''​\\ ''​echo "​summary:"''​\\ ''​echo''​\\ ''​ps -ef''​\\ ''​echo''​\\ ''​echo "​lines:​ $(ps -ef | wc -l)"''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script connects to all the servers whose IP addresses are specified in "​servers"​ file,\\ # remotely executes commands "​hostname"​ and "uname -a" and displays the result in STDOUT.\\ # Valid rsa/dsa authentication keys for ssh are mandatory.''​\\ ''​for a in $(cat servers); do''​\\ ''​ssh $a '​hostname;​ uname -a'; done''​ | | **for** <​variable>​ **in** <​string>​**;​** **do** <​list>​**;​ done** | assigns a variable one by one the value of all parameters of the script or a particular string and executes a list of commands\\ ''#​!/​bin/​bash''​\\ ''#​ The script creates users specified in "​users"​ file in the working directory,​\\ # adds them to "​admins"​ group and assigns "​password"​ as their password.''​\\ ''​for user in $(cat users); do''​\\ <​tab>''​useradd -g admins $user''​\\ <​tab>''​echo password | passwd --stdin $user''​\\ ''​done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script prints the contents of the working directory.''​\\ ''​for x in $(ls -F)''​\\ ''​do''​\\ <​tab>''​echo "​Directory `pwd` contains file/​directory:​ $x"''​\\ ''​done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script counts the number of items in the working directory.''​\\ ''​POC=0''​\\ ''​for name in *''​\\ <​tab>''​do''​\\ <​tab><​tab>''​POC=$%%(($POC+1))%%''​\\ <​tab>''​done''​\\ ''​echo "​Directory `pwd` contains $POC items."''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script converts the format of all .tif files to .jpg.''​\\ ''​for pic in *.tif; do''​\\ <​tab>''​convert "​$pic"​ "​$(echo "​$pic"​ | sed '​s/​\.tif/​.jpg/'​)"''​\\ ''​done''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script checks which of the particular processes are running, prints their names\\ # and the number of lines they occupy and finally prints all running processes\\ # and the total number of lines.''​\\ ''​services="​http pmon lsnr sap"''​\\ ''​for service in $services''​\\ <​tab>''​do running_processes="​$(ps -ef | grep $service | grep -v grep)"''​\\ <​tab><​tab>''​if [ -n "​$running_processes"​ ]''​\\ <​tab><​tab><​tab>''​then echo''​\\ <​tab><​tab><​tab>''​echo $service:''​\\ <​tab><​tab><​tab>''​echo''​\\ <​tab><​tab><​tab>''​echo "​$running_processes"''​\\ <​tab><​tab><​tab>''​echo''​\\ <​tab><​tab><​tab>''​echo "​lines:​ $(echo "​$running_processes"​ | wc -l)"''​\\ <​tab><​tab><​tab>''​echo''​\\ <​tab><​tab><​tab>''​echo "​=========="''​\\ <​tab><​tab>''​fi''​\\ <​tab>''​done''​\\ ''​echo''​\\ ''​echo "​summary:"''​\\ ''​echo''​\\ ''​ps -ef''​\\ ''​echo''​\\ ''​echo "​lines:​ $(ps -ef | wc -l)"''​\\ \\ ''#​!/​bin/​bash''​\\ ''#​ The script connects to all the servers whose IP addresses are specified in "​servers"​ file,\\ # remotely executes commands "​hostname"​ and "uname -a" and displays the result in STDOUT.\\ # Valid rsa/dsa authentication keys for ssh are mandatory.''​\\ ''​for a in $(cat servers); do''​\\ ''​ssh $a '​hostname;​ uname -a'; done''​ |