Bash scripts: start
Bash scripts, part 2: loops
Bash scripts, part 3: command line options and keys
Bash scripts, part 4: input and output
Bash Scripts, Part 5: Signals, Background Tasks, Script Management
Bash scripts, part 6: functions and library development
Bash scripts, part 7: sed and word processing
Bash scripts, part 8: awk data processing language
Bash scripts, part 9: regular expressions
Bash scripts, part 10: practical examples
Bash scripts, part 11: expect and automate interactive utilities


$ ./myscript 10 20 $0 β name of the script.$1 β first parameter.$2 β second parameter - and so on, right down to the $9 variable, in which the ninth parameter falls. #!/bin/bash echo $0 echo $1 echo $2 echo $3  ./myscript 5 10 15 
 #!/bin/bash total=$[ $1 + $2 ] echo The first parameter is $1. echo The second parameter is $2. echo The sum is $total. 
 #!/bin/bash echo Hello $1, how do you do  ./myscript Adam 
 ${10}  #!/bin/bash if [ -n "$1" ] then echo Hello $1. else echo "No parameters found. " fi 
$# variable contains the number of parameters passed to the script when invoked. #!/bin/bash echo There were $# parameters passed.  ./myscript 1 2 3 4 5 
 #!/bin/bash echo The last parameter was ${!#} 
$* and $@ . Both contain all command line parameters, which makes it possible to access what is passed to the script, without using positional parameters.$* contains all parameters entered on the command line as a single βwordβ.$@ parameters are divided into separate "words". These parameters can be iterated in cycles. #!/bin/bash echo "Using the \$* method: $*" echo "-----------" echo "Using the \$@ method: $@" 
 #!/bin/bash count=1 for param in "$*" do echo "\$* Parameter #$count = $param" count=$(( $count + 1 )) done count=1 for param in "$@" do echo "\$@ Parameter #$count = $param" count=$(( $count + 1 )) done 
$* variable contains all parameters passed to the script as a single piece of data, while in the $@ variable they are represented by independent values. Which variable to use depends on what exactly is needed in a particular scenario.shift command in bash scripts with caution, since it literally shifts the values ββof the positional parameters.$3 becomes the value of the variable $2 , the value of $2 becomes $1 , and what was before in $1, is lost. Note that the value of the $0 variable that contains the name of the script does not change.shift command, we will consider another way to iterate through the parameters passed to the script: #!/bin/bash count=1 while [ -n "$1" ] do echo "Parameter #$count = $1" count=$(( $count + 1 )) shift done while , checking the length of the value of the first parameter. When the length is equal to zero, the loop is exited. After checking the first parameter and displaying it on the screen, the shift command is called, which shifts the parameter values ββby one position.
shift command, keep in mind that each time it is called, the value of the $1 variable is permanently lost. #!/bin/bash echo while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option" ;; -b) echo "Found the -b option" ;; -c) echo "Found the -c option" ;; *) echo "$1 is not an option" ;; esac shift done  $ ./myscript βa βb βc βd 
case construction, which compares the key passed to it with the list of keys processed by the script. If the transferred value was in this list, the corresponding branch of the code is executed. If at the call of the script any key is used, the processing of which is not provided for, the β*β branch will be executed. #!/bin/bash while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option" ;; -b) echo "Found the -b option";; -c) echo "Found the -c option" ;; --) shift break ;; *) echo "$1 is not an option";; esac shift done count=1 for param in $@ do echo "Parameter #$count: $param" count=$(( $count + 1 )) done break command to interrupt the while when it detects a double dash in the string.
 ./myscript -a test1 -b -c test2  #!/bin/bash while [ -n "$1" ] do case "$1" in -a) echo "Found the -a option";; -b) param="$2" echo "Found the -b option, with parameter value $param" shift ;; -c) echo "Found the -c option";; --) shift break ;; *) echo "$1 is not an option";; esac shift done count=1 for param in "$@" do echo "Parameter #$count: $param" count=$(( $count + 1 )) done  ./myscript -a -b test1 -d 
case construction handles three keys. The -b switch requires an additional parameter. Since the key being processed is in the $1 variable, the corresponding parameter will be in $2 (the shift command is used here, therefore, as it is processed, everything that is passed to the script is shifted to the left). When we dealt with this, it remains only to extract the value of the $2 variable and we will have the parameter of the desired key. Of course, we need another shift command here so that the next key will fall into $1 .-aDisplay all objects.-cPerform a count.-dSpecify the directory.-eExpand the object.-fSpecify the file from which to read the data.-hPrint help on the command.-iIgnore case.-lPerform full-format output.-nUse non-interactive (batch) mode.-oAllows you to specify the file to which to redirect the output.-qRun the script in quiet mode.-rProcess folders and files recursively.-sRun the script in silent mode.-vRun verbose output.-xExclude object.-yAnswer βyesβ to all questions.
read command. #!/bin/bash echo -n "Enter your name: " read name echo "Hello $name, welcome to my program." echo command, which displays the prompt, is invoked with the -n key. This leads to the fact that at the end of the invitation does not display a newline character, which allows the script user to enter data in the same place where the invitation is located, and not on the next line.
read you can specify several variables: #!/bin/bash read -p "Enter your name: " first last echo "Your data for $last, $firstβ¦" 
read , you do not specify a variable, the data entered by the user will be placed in a special environment variable REPLY : #!/bin/bash read -p "Enter your name: " echo Hello $REPLY, welcome to my program. 
read command, you can use the -t key. Namely, the key parameter sets the input wait time in seconds: #!/bin/bash if read -t 5 -p "Enter your name: " name then echo "Hello $name, welcome to my script" else echo "Sorry, too slow! " fi else , displaying an apology.
-s key of the read command prevents the display of data entered from the keyboard. In fact, the data is output, but the read command makes the text color the same as the background color. #!/bin/bash read -s -p "Enter your password: " pass echo "Is your password really $pass? " 
read command can, on every call, read one line of text from a file. When there are no more unread lines in the file, it will simply stop. If you need to get all the contents of the file in the script, you can use the pipeline to transfer the results of calling the cat for the file, the while construct that contains the read command (of course, using the cat looks primitive, but our goal is to show everything as simple as possible, newbies; experienced users sure will understand this). #!/bin/bash count=1 cat myfile | while read line do echo "Line $count: $line" count=$(( $count + 1 )) done echo "Finished" 
while contents of the file and went through all the lines of this file, displaying the number and contents of each of them.
Source: https://habr.com/ru/post/326328/
All Articles