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/07 23:00]
Miroslav Bernát
scripts-and-functions [2019/10/08 00:26] (current)
Miroslav Bernát
Line 13: Line 13:
 | **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>) | 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''​ | | **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 not specified, the return code of the script is the return code of the last command within the script |+| **exit** (<n>) | exits a script with the specified return code, if not specified, the return code of the script is the return code of the last command ​executed ​within the script |
 \\ \\
 ^ 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 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"''​\\ ''>​ }''​ | | **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 not specified, the return ​code of the function ​is the return code of the last command executed ​within ​the function |
Last modified: 2019/10/08 00:26 by Miroslav Bernát

visits: