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
No comments:
Post a Comment