if [ <some test> ]
then
<commands>
fi
The square brackets ( [ ] ) in the if statement above are actually a reference to the command test. This means that all of the operators that test allows may be used here as well. Look up the man page for test to see all of the possible operators (there are quite a few) but some of the more common ones are listed below.
Operator | Description |
---|---|
! EXPRESSION | The EXPRESSION is false. |
-n STRING | The length of STRING is greater than zero. |
-z STRING | The lengh of STRING is zero (ie it is empty). |
STRING1 = STRING2 | STRING1 is equal to STRING2 |
STRING1 != STRING2 | STRING1 is not equal to STRING2 |
INTEGER1 -eq INTEGER2 | INTEGER1 is numerically equal to INTEGER2 |
INTEGER1 -gt INTEGER2 | INTEGER1 is numerically greater than INTEGER2 |
INTEGER1 -lt INTEGER2 | INTEGER1 is numerically less than INTEGER2 |
-d FILE | FILE exists and is a directory. |
-e FILE | FILE exists. |
-r FILE | FILE exists and the read permission is granted. |
-s FILE | FILE exists and it's size is greater than zero (ie. it is not empty). |
-w FILE | FILE exists and the write permission is granted. |
-x FILE | FILE exists and the execute permission is granted. |
- = is slightly different to -eq. [ 001 = 1 ] will return false as = does a string comparison (ie. character for character the same) whereas -eq does a numerical comparison meaning [ 001 -eq 1 ] will return true.
- When we refer to FILE above we are actually meaning a path. Remember that a path may be absolute or relative and may refer to a file or a directory.
- Because [ ] is just a reference to the command test we may experiment and trouble shoot with test on the command line to make sure our understanding of its behaviour is correct.
test is available in coreutils package
exampe using test :
test 001 = 1
echo $?
test 001 -eq 1
echo $?
touch myfile
test -s myfile
echo $?
ls /etc > myfile
test -s myfile
echo $?
The variable $? holds the exit status of the previously run command (in this case test). 0 means TRUE (or success). 1 = FALSE (or failure).
There aren't any rules regarding indenting in Bash so you may indent or not indent however you like
indent for readability
This is a light variation on the if statement. If we would like to check an expression then we may use the double brackets just like we did for variables.
if [ <some test> ]
then
<commands>
else
<other commands>
fi
if [ $# -eq 1 ]
if [ <some test> ]
then
<commands>
elif [ <some test> ]
then
<different commands>
else
<other commands>
fi
boolean operators
- and - &&
- or - ||
if [ -r $1 ] && [ -s $1 ]
case <variable> in
<pattern 1>)
<commands>
;;
<pattern 2>)
<other commands>
;;
esac
variable in bash
- $0 - The name of the Bash script.
- $1 - $9 - The first 9 arguments to the Bash script. (As mentioned above.)
- $# - How many arguments were passed to the Bash script.
- $@ - All the arguments supplied to the Bash script.
- $? - The exit status of the most recently run process.
- $$ - The process ID of the current script.
- $USER - The username of the user running the script.
- $HOSTNAME - The hostname of the machine the script is running on.
- $SECONDS - The number of seconds since the script was started.
- $RANDOM - Returns a different random number each time is it referred to.
- $LINENO - Returns the current line number in the Bash script.
var=value
- Single quotes will treat every character literally.
- Double quotes will allow you to do substitution (that is include variables within the setting of the value).
Command substitution
myvar=$( ls /etc | wc -l )
Command substitution is nice and simple if the output of the command is a single word or line. If the output goes over several lines then the newlines are simply removed and all the output ends up on a single line.
Remember how in the previous section we talked about scripts being run in their own process? This introduces a phenomenon known as scope which affects variables amongst other things. The idea is that variables are limited to the process they were created in. Normaly this isn't an issue but sometimes, for instance, a script may run another script as one of its commands. If we want the variable to be available to the second script then we need to export the variable.
running script
. = current directory
.. = one level up
by relative path ./script
by absoulte path /home/user/script
shebang #!
must be on the very first line of the file must also be no spaces before the # or between the ! and the path to the interpreter.
https://ryanstutorials.net/bash-scripting-tutorial/bash-variables.php
tput is a command which allows you to control the cursor on the terminal and the format of content that is printed.
input_length=${#message}
Find out how many characters are in the string message. We had to assign all the input values to the variable message first as ${#@} would tell us how many command line arguments there were instead of the number of characters combined
tput cols will tell us how many columns the terminal has.
tput lines tell us how many lines (or rows) the terminal has.
tput cup will place the cursor at the given row and column.
tput cup $middle_row $middle_col
tput bold
tput sgr0 turn off bold
src
https://ryanstutorials.net/bash-scripting-tutorial/
Comments
Post a Comment