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/04/12 20:42]
Miroslav Bernát
scripts-and-functions [2019/04/12 21:15] (current)
Miroslav Bernát
Line 12: Line 12:
 | **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''​ |
 | **break** (<n>) | exits from within a loop, //n// specifies the number of loops\\ ''#​!/​bin/​bash''​\\ ''#​ The script exits if the value of the variable is equal to 3.''​\\ ''​for myloop in 1 2 3 4 5; do''​\\ <​tab>''​echo "​$myloop"''​\\ <​tab>''​if [ "​$myloop"​ -eq 3 ]; then''​\\ <​tab>''​break # This line will break out of the loop; fi''​\\ ''​done''​ | | **break** (<n>) | exits from within a loop, //n// specifies the number of loops\\ ''#​!/​bin/​bash''​\\ ''#​ The script exits if the value of the variable is equal to 3.''​\\ ''​for myloop in 1 2 3 4 5; do''​\\ <​tab>''​echo "​$myloop"''​\\ <​tab>''​if [ "​$myloop"​ -eq 3 ]; then''​\\ <​tab>''​break # This line will break out of the loop; fi''​\\ ''​done''​ |
-| **continue** (<n>) | repeates ​within a loop, //n// specifies the number of loops\\ ''#​!/​bin/​bash''​\\ ''#​ The script allows only small letters in file names.''​\\ ''​LIST="​$(ls)"''​\\ ''​for name in "​$LIST";​ do''​\\ <​tab>''​if %%[[ "​$name"​ != *[[:​upper:​]]* ]]%%; then''​\\ <​tab>''​continue;​ fi''​\\ \\ ''​ORIG="​$name"''​\\ ''​NEW=$(echo $name | tr '​A-Z'​ '​a-z'​)''​\\ ''​mv "​$ORIG"​ "​$NEW"''​\\ ''​echo "new name for $ORIG is $NEW"''​\\ ''​done''​ |+| **continue** (<n>) | repeats ​within a loop, //n// specifies the number of loops\\ ''#​!/​bin/​bash''​\\ ''#​ The script allows only small letters in file names.''​\\ ''​LIST="​$(ls)"''​\\ ''​for name in "​$LIST";​ do''​\\ <​tab>''​if %%[[ "​$name"​ != *[[:​upper:​]]* ]]%%; then''​\\ <​tab>''​continue;​ fi''​\\ \\ ''​ORIG="​$name"''​\\ ''​NEW=$(echo $name | tr '​A-Z'​ '​a-z'​)''​\\ ''​mv "​$ORIG"​ "​$NEW"''​\\ ''​echo "new name for $ORIG is $NEW"''​\\ ''​done''​ |
 | **exit** (<n>) | exits a script with the specified return code, if it is omitted, the return status is that of the last command executed in the script body | | **exit** (<n>) | exits a script with the specified return code, if it is omitted, the return status is that of the last command executed in the script body |
 \\ \\
Last modified: 2019/04/12 21:15 by Miroslav Bernát

visits: