Saturday, November 7, 2020

Notes to self - about Bash and related things

Using Back Ticks to provide value to a variable.
For example:

[root@localhost bash_scripting]# cat ./19-3.sh
#!/bin/bash
# run this script with a few arguments
echo "You have entered $# arguments."
for i in "$@"
        do
                echo $i
        done
exit 0
[root@localhost bash_scripting]#

Follow by word count from 19-3.sh

[root@localhost bash_scripting]# wc -l ./19-3.sh
8 ./19-3.sh
[root@localhost bash_scripting]#

To apply the value 8 to a variable Lines, use the following command:

[root@localhost bash_scripting]# Lines=`wc -l ./19-3.sh`
[root@localhost bash_scripting]# echo $Lines
8 ./19-3.sh
[root@localhost bash_scripting]#

Another way for doing this is to use double quotes and brace.

[root@localhost bash_scripting]# ls
19-10.sh  19-3.sh  19-4.sh  19-6.sh  19-6_1.sh  BROWSER.sh  ELIF.sh  IF_AND.sh
[root@localhost bash_scripting]# wc -l ./19-4.sh
12 ./19-4.sh
[root@localhost bash_scripting]# New_Lines="$(wc -l ./19-4.sh)"
[root@localhost bash_scripting]# echo $New_Lines
12 ./19-4.sh
[root@localhost bash_scripting]#

To use arithmetic in Bash script

[root@localhost bash_scripting]# num1=expr "28.5"
-bash: 28.5: command not found
[root@localhost bash_scripting]# num1=expr "28"
-bash: 28: command not found
[root@localhost bash_scripting]# num1=expr 28
-bash: 28: command not found
[root@localhost bash_scripting]# num1=`expr 28.5`
[root@localhost bash_scripting]# num2=`expr 37.9`
[root@localhost bash_scripting]# add1=`expr $num1 + $num2`
expr: non-integer argument
[root@localhost bash_scripting]# num1=`expr 28`
[root@localhost bash_scripting]# num2=`expr 37`
[root@localhost bash_scripting]# add1=`expr $num1 + $num2`
[root@localhost bash_scripting]# echo $add1
65
[root@localhost bash_scripting]#

To use multiplication in Bash script
Note:The multiplication operator * must be escaped when used in an arithmetic expression with expr.

[root@localhost bash_scripting]# mul1=`expr $num1 * $num2`
expr: syntax error: unexpected argument ‘19-10.sh’
[root@localhost bash_scripting]# mul1=`expr $num1 \* $num2`
[root@localhost bash_scripting]# echo $mul1
1036
[root@localhost bash_scripting]#

To use comparison in Bash

[root@localhost bash_scripting]# x=10
[root@localhost bash_scripting]# y=20
[root@localhost bash_scripting]# res=`expr $x = $y`
[root@localhost bash_scripting]# echo $res
0
[root@localhost bash_scripting]# res1=`expr $x < $y`
-bash: 20: No such file or directory
[root@localhost bash_scripting]#
[root@localhost bash_scripting]# x=`expr 10`
[root@localhost bash_scripting]# y=`expr 20`
[root@localhost bash_scripting]# res1=$x < $y
-bash: 20: No such file or directory
[root@localhost bash_scripting]# res1=`expr $x < $y`
-bash: 20: No such file or directory
[root@localhost bash_scripting]# res1=`expr $x \< $y`
[root@localhost bash_scripting]# echo res1
res1
[root@localhost bash_scripting]# echo $res1
1
[root@localhost bash_scripting]# res2=`expr $x \!= $y`
[root@localhost bash_scripting]# echo $res2
1
[root@localhost bash_scripting]#

Sunday, November 1, 2020

Conditional Loop - For Loop

The following is the for loop to get ping result from host 192.168.56.101 to 192.168.56.104.

#!/bin/bash
touch ~/Documents/bash_script/ping_result
for i in {104..101}
do
    ping 192.168.56.$i >>~/Documents/bash_script/ping_result && echo 192.168.56.$i is up
done

Important notes:
If the remote host is pingable, the echo $? will return 0.
If the remote host is not pingable, the echo $? will return 1.




This script will generate a new file "ping_result" and all the ping result will be stored in this file.









Let's find out why only the pingable host will result in echo 192.168.56.$i is up.