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
Next revision
Previous revision
scripts-and-functions [2019/04/12 21:15]
Miroslav Bernát
scripts-and-functions [2019/08/02 13:01] (current)
Miroslav Bernát
Line 4: Line 4:
 | **true** | returns a successful (zero) return code | | **true** | returns a successful (zero) return code |
 | **false** | returns an unsuccessful (non-zero) return code | | **false** | returns an unsuccessful (non-zero) return code |
-| **test** <​condition>​ / **[** <​condition>​ **]** / **[****[** <​condition>​ **]****]** | evaluates a specified condition and based on its result a successful or unsuccessful return code is provided; to verify the existence of particular files specific options are used with a file name as a parameter ​– e.g. **-e** any file, **-f** regular file, **-b** block device, **-c** character device, **-d** directory, **-h** symbolic link, **-s** file with non-zero size, **-r** file with read permissions,​ **-w** file with write permissions,​ **-x** executable file, **-u** file with SUID bit, **-g** file with SGID bit, **-k** file with a sticky bit, <​file1>​ **-nt** <​file2>​ file1 is newer than file2, <​file1>​ **-ot** <​file2>​ file1 is older than file2; to verify numeric values the following operators are used **-eq** is equal, **-ne** is not equal, **-lt** less than, **-le** less than or equal, **-gt** greater than, **-ge** greater than or equal; to verify strings **=** is equal, **!=** is not equal, **-z** <​string>​ the length of string is zero, **-n** <​string>​ the length of string is non-zero; **!** <​expression>​ is used for logical negation of a specified expression; **[****[** **]****]** provide more extended expression testing such as: <​string>​ **=** / **==** <​pattern>,​ <​string>​ **!=** <​pattern>,​ <​string1>​ **<** <​string2>​ or <​string1>​ **>** <​string2>​ (string1 is lexicographically before or after string2) and compound expressions like **&&​** to evaluate the second expression if the first is true, **||** to evaluate the second expression if the first is false and **()** to group expressions\\ ''​$ test -f *.txt''​ / ''​[ -f *.txt ]''​\\ (return code is 0 if only one file with „.txt“ suffix exists)\\ ''​$ %%[[ -n $(find . -name "​*.txt"​) ]]%%''​\\ (return code is 0 if at least one file with „.txt“ suffix exists)\\ ''​$ %%[[ abc == *c ]]%%''​\\ ''​$ %%[[ a < z ]]%%''​\\ ''​$ [ $PWD = $HOME ] && echo "​home"​ || pwd''​\\ ''​$ %%[[ -n $(rpm -qa | egrep "​^sudo"​) && $(stat -c %s /​etc/​sudoers) -gt 0 ]]%% && echo "Sudo is used." || echo "Sudo is not used."''​\\ ''#​ duid=$(awk -F ":"​ '​{print $3}' /etc/passwd | sort | uniq -d); %%[[ -z "​$duid"​ ]]%% && echo "​NONE"​ || echo "​$duid"''​\\ (prints duplicate UID in the system)\\ ''#​ pp=$(awk -F ":"​ '{if (length($2) > 2) print $1}' /​etc/​passwd);​ %%[[ -z "​$pp"​ ]]%% && echo "​NONE"​ || echo "​$pp"​ | tr " " "​\n"''​\\ (prints users whose passwords are stored in ///​etc/​passwd//​) |+| **test** <​condition>​ / **[** <​condition>​ **]** / **[****[** <​condition>​ **]****]** | evaluates a specified condition and based on its result a successful or unsuccessful return code is provided; to verify the existence of particular files specific options are used with a file name as an argument ​– e.g. **-e** any file, **-f** regular file, **-b** block device, **-c** character device, **-d** directory, **-h** symbolic link, **-s** file with non-zero size, **-r** file with read permissions,​ **-w** file with write permissions,​ **-x** executable file, **-u** file with SUID bit, **-g** file with SGID bit, **-k** file with a sticky bit, <​file1>​ **-nt** <​file2>​ file1 is newer than file2, <​file1>​ **-ot** <​file2>​ file1 is older than file2; to verify numeric values the following operators are used **-eq** is equal, **-ne** is not equal, **-lt** less than, **-le** less than or equal, **-gt** greater than, **-ge** greater than or equal; to verify strings **=** is equal, **!=** is not equal, **-z** <​string>​ the length of string is zero, **-n** <​string>​ the length of string is non-zero; **!** <​expression>​ is used for logical negation of a specified expression; **[****[** **]****]** provide more extended expression testing such as: <​string>​ **=** / **==** <​pattern>,​ <​string>​ **!=** <​pattern>,​ <​string1>​ **<** <​string2>​ or <​string1>​ **>** <​string2>​ (string1 is lexicographically before or after string2) and compound expressions like **&&​** to evaluate the second expression if the first is true, **||** to evaluate the second expression if the first is false and **()** to group expressions\\ ''​$ test -f *.txt''​ / ''​[ -f *.txt ]''​\\ (return code is 0 if only one file with „.txt“ suffix exists)\\ ''​$ %%[[ -n $(find . -name "​*.txt"​) ]]%%''​\\ (return code is 0 if at least one file with „.txt“ suffix exists)\\ ''​$ %%[[ abc == *c ]]%%''​\\ ''​$ %%[[ a < z ]]%%''​\\ ''​$ [ $PWD = $HOME ] && echo "​home"​ || pwd''​\\ ''​$ %%[[ -n $(rpm -qa | egrep "​^sudo"​) && $(stat -c %s /​etc/​sudoers) -gt 0 ]]%% && echo "Sudo is used." || echo "Sudo is not used."''​\\ ''#​ duid=$(awk -F ":"​ '​{print $3}' /etc/passwd | sort | uniq -d); %%[[ -z "​$duid"​ ]]%% && echo "​NONE"​ || echo "​$duid"''​\\ (prints duplicate UID in the system)\\ ''#​ pp=$(awk -F ":"​ '{if (length($2) > 2) print $1}' /​etc/​passwd);​ %%[[ -z "​$pp"​ ]]%% && echo "​NONE"​ || echo "​$pp"​ | tr " " "​\n"''​\\ (prints users whose passwords are stored in ///​etc/​passwd//​) |
 | **if** <​condition=true>​**;​** **then** <​action>​**;​** (**elif** <​condition2=true>​**;​ then** <​action2>​**;​**) (**else** <​action3>​)**;​ fi** | **if** specifies a condition, if it is true, then **then** specifies a required action, otherwise **else** specifies an alternative action, additional conditions can be specified by **elif**, the end of all conditions is closed with **fi**\\ ''​$ if test -f *.txt / [ -f *.txt ]; then echo "file exists";​ fi''​\\ ''​$ if [ "​$(whoami)"​ != '​root'​ ]; then exit; else echo "​hello";​ fi''​\\ ''​$ if [ $? -ne 0 ]; then echo $?; fi''​\\ (return code is 0 if the last command finished unsuccessfully)\\ ''#​ np=$(awk -F ":"​ '{if (length($2) > 2 && $2 !~ /​^(!!)?​(\$[1256]\$)/​) print $1}' /​etc/​shadow);​ if %%[[ -z "​$np"​ ]]%%; then echo "All passwords are encrypted.";​ else { echo "​Password may not be encrypted or is incorrect for user:";​ echo "​$np"​ | tr " " "​\n";​};​ fi''​\\ (prints users with not encrypted or incorrect passwords) | | **if** <​condition=true>​**;​** **then** <​action>​**;​** (**elif** <​condition2=true>​**;​ then** <​action2>​**;​**) (**else** <​action3>​)**;​ fi** | **if** specifies a condition, if it is true, then **then** specifies a required action, otherwise **else** specifies an alternative action, additional conditions can be specified by **elif**, the end of all conditions is closed with **fi**\\ ''​$ if test -f *.txt / [ -f *.txt ]; then echo "file exists";​ fi''​\\ ''​$ if [ "​$(whoami)"​ != '​root'​ ]; then exit; else echo "​hello";​ fi''​\\ ''​$ if [ $? -ne 0 ]; then echo $?; fi''​\\ (return code is 0 if the last command finished unsuccessfully)\\ ''#​ np=$(awk -F ":"​ '{if (length($2) > 2 && $2 !~ /​^(!!)?​(\$[1256]\$)/​) print $1}' /​etc/​shadow);​ if %%[[ -z "​$np"​ ]]%%; then echo "All passwords are encrypted.";​ else { echo "​Password may not be encrypted or is incorrect for user:";​ echo "​$np"​ | tr " " "​\n";​};​ fi''​\\ (prints users with not encrypted or incorrect passwords) |
 | **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''​ |
Line 16: Line 16:
 \\ \\
 ^ FUNCTIONS ^ ^ ^ FUNCTIONS ^ ^
-| **function** <​function_name>​ **{** <​list>​**;​}** / <​function_name>​ **()** **{** <​list>​**;​}** | defines a function whose contents is enclosed in braces; the function is used for frequent execution of particular commands and it is launched by its name in the current shell returning a status code of the last executed command provided there is no other specified by „return“ command; a local variable (which is inherited also into subfunctions) can be defined by „local“ command inside the function, if a global variable of the same name exists, it is suppressed in the function; a function can be used permanently (in future shell executions) if it is specified in //​.bashrc//;​ information about forwarded arguments to scripts or functions are saved in special variables („$#“ total number of arguments, „$<​n>​“ particular argument in the order in a range of 1–9, „${<​n>​}“ any argument in the order, „$*“ all arguments)\\ ''#​ Function „test“ creates a file which name is specified by the argument and sets\\ # particular permissions for it. If a file of the given name already exists or the name\\ # contains just space or is missing an error message is displayed.''​\\ ''​$ function test''​\\ ''>​ {''​\\ ''>​ if ! [ -e $* || $* = ('​)('​) ]; then''​\\ ''>''<​tab>''​touch $*; chmod 755 $*''​\\ ''>​ else''​\\ ''>''<​tab>''​echo "​Error"''​\\ ''>​ fi''​\\ ''>​ echo "​Number of arguments: $#"''​\\ ''>​ echo "All arguments: $*"''​\\ ''>​ echo "Fifth argument: $5"''​\\ ''>​ }''​\\ \\ ''#​ Function „user“ checks if a specified user given by the argument exists in the system,\\ # if so it prints related detailed information.''​\\ ''​$ user ()''​\\ ''>​ {''​\\ ''>​ if [ $(grep -ic ''''​^$1''​ ''/​etc/​passwd) -eq 0 ]; then''​\\ ''>''<​tab>''​echo "User $1 does not exist."''​\\ ''>​ else''​\\ ''>''<​tab>''​echo "User $1 exists."​ && finger $1''​\\ ''>​ fi''​\\ ''>​ }''​\\ \\ ''#​ Function „poc“ computes a percentage value of the second numeric argument\\ # from the first one, provided the first argument represents value 100 %.''​\\ ''​$ poc () {''​\\ ''>​ local x=$1 y=$2''​\\ ''>​ echo "$x = 100%, $y = x%"''​\\ ''>​ echo "$y = $[$%%(( 100 * $y ))%% / $x]% z $x"''​\\ ''>​ }''​ |+| **function** <​function_name>​ **{** <​list>​**;​}** / <​function_name>​ **()** **{** <​list>​**;​}** | defines a function whose contents is enclosed in braces; the function is used for frequent execution of particular commands and it is launched by its name in the current shell returning a status code of the last executed command provided there is no other specified by „return“ command; a local variable (which is inherited also into subfunctions) can be defined by „local“ command inside the function, if a global variable of the same name exists, it is suppressed in the function; a function can be used permanently (in future shell executions) if it is specified in //​.bashrc//;​ information about forwarded arguments to scripts or functions are saved in special variables („$#“ total number of arguments, „$<​n>​“ particular argument in the order in a range of 1–9, „${<​n>​}“ any argument in the order, „$*“ all arguments)\\ ''#​ Function „test“ creates a file which name is specified by the argument and sets\\ # particular permissions for it. If a file of the specified ​name already exists or the name\\ # contains just space or is missing an error message is displayed.''​\\ ''​$ function test''​\\ ''>​ {''​\\ ''>​ if ! [ -e $* || $* = ('​)('​) ]; then''​\\ ''>''<​tab>''​touch $*; chmod 755 $*''​\\ ''>​ else''​\\ ''>''<​tab>''​echo "​Error"''​\\ ''>​ fi''​\\ ''>​ echo "​Number of arguments: $#"''​\\ ''>​ echo "All arguments: $*"''​\\ ''>​ echo "Fifth argument: $5"''​\\ ''>​ }''​\\ \\ ''#​ Function „user“ checks if a specified user given by the argument exists in the system,\\ # if so it prints related detailed information.''​\\ ''​$ user ()''​\\ ''>​ {''​\\ ''>​ if [ $(grep -ic ''''​^$1''​ ''/​etc/​passwd) -eq 0 ]; then''​\\ ''>''<​tab>''​echo "User $1 does not exist."''​\\ ''>​ else''​\\ ''>''<​tab>''​echo "User $1 exists."​ && finger $1''​\\ ''>​ fi''​\\ ''>​ }''​\\ \\ ''#​ Function „poc“ computes a percentage value of the second numeric argument\\ # from the first one, provided the first argument represents value 100 %.''​\\ ''​$ poc () {''​\\ ''>​ local x=$1 y=$2''​\\ ''>​ echo "$x = 100%, $y = x%"''​\\ ''>​ echo "$y = $[$%%(( 100 * $y ))%% / $x]% z $x"''​\\ ''>​ }''​ |
 | **return** (<n>) | exits a function with the specified return code, if it is omitted, the return status is that of the last command executed in the function body | | **return** (<n>) | exits a function with the specified return code, if it is omitted, the return status is that of the last command executed in the function body |
Last modified: 2019/04/12 21:15 by Miroslav Bernát

visits: