[ Pobierz całość w formacie PDF ]
variable, and executes the file-name value mentioned in the ENV variable.
Hack 85. How to generate random number in bash
shell
Use the $RANDOM bash built-in function to generate random number between
0 32767 as shown below.
$ echo $RANDOM
106
Linux 101 Hacks www.thegeekstuff.com
22543
$ echo $RANDOM
25387
$ echo $RANDOM
647
Hack 86. Debug a shell script
To debug a shell script use set xv inside the shell script at the top.
Shell script with no debug command:
$ cat filesize.sh
#!/bin/bash
for filesize in $(ls -l . | grep "^-" | awk '{print
$5}')
do
let totalsize=$totalsize+$filesize
done
echo "Total file size in current directory: $totalsize"
Output of Shell script with no debug command:
$ ./filesize.sh
Total file size in current directory: 652
Shell script with Debug command inside:
Add set xv inside the shell script now to debug the output as shown below.
$ cat filesize.sh
#!/bin/bash
set -xv
for filesize in $(ls -l . | grep "^-" | awk '{print
107
Linux 101 Hacks www.thegeekstuff.com
$5}')
do
let totalsize=$totalsize+$filesize
done
echo "Total file size in current directory: $totalsize"
Output of Shell script with Debug command inside:
$ ./fs.sh
++ ls -l .
++ grep '^-'
++ awk '{print $5}'
+ for filesize in '$(ls -l . | grep "^-" | awk
'\''{print $5}'\'')'
+ let totalsize=+178
+ for filesize in '$(ls -l . | grep "^-" | awk
'\''{print $5}'\'')'
+ let totalsize=178+285
+ for filesize in '$(ls -l . | grep "^-" | awk
'\''{print $5}'\'')'
+ let totalsize=463+189
+ echo 'Total file size in current directory: 652'
Total file size in current directory: 652
Execute Shell script with debug option:
Instead of giving the set xv inside the shell script, you can also provide that
while executing the shell script as shown below.
$ bash -xv filesize.sh
Hack 87. Quoting
echo statement without any special character.
$ echo The Geek Stuff
108
Linux 101 Hacks www.thegeekstuff.com
The Geek Stuff
Echo statement with a special character ; . semi-colon is a command
terminator in bash. In the following example, The Geek works for the echo
and Stuff is treated as a separate Linux command and gives command not
found.
$ echo The Geek; Stuff
The Geek
-bash: Stuff: command not found
To avoid this you can add a \ in front of semi-colon, which will remove the
special meaning of semi-colon and just print it as shown below.
$ echo The Geek\; Stuff
The Geek; Stuff
Single Quote
Use single quote when you want to literally print everything inside the single
quote. Even the special variables such as $HOSTNAME will be print as
$HOSTNAME instead of printing the name of the Linux host.
$ echo 'Hostname=$HOSTNAME ; Current User=`whoami` ;
Message=\$ is USD'
Hostname=$HOSTNAME ; Current User=`whoami` ;
Message=\$ is USD
Double Quote
Use double quotes when you want to display the real meaning of special
variables.
$ echo "Hostname=$HOSTNAME ; Current User=`whoami` ;
Message=\$ is USD"
109
Linux 101 Hacks www.thegeekstuff.com
Hostname=dev-db ; Current User=ramesh ; Message=$ is
USD
Double quotes will remove the special meaning of all characters except the
following:
o $ Parameter Substitution.
o ` Backquotes
o \$ Literal Dollar Sign.
o \´ Literal Backquote.
o \" Embedded Doublequote.
o \\ Embedded Backslashes.
Hack 88. Read data file fields inside a shell script
This example shows how to read a particular field from a data-file and
manipulate it inside a shell-script. For example, let us assume the
employees.txt file is in the format of {employee-name}:{employee-
id}:{department-name}, with colon delimited file as shown below.
$ cat employees.txt
Emma Thomas:100:Marketing
Alex Jason:200:Sales
Madison Randy:300:Product Development
Sanjay Gupta:400:Support
Nisha Singh:500:Sales
The following shell script explains how to read specific fields from this
employee.txt file.
$ vi read-employees.sh
#!/bin/bash
IFS=:
echo "Employee Names:"
echo "---------------"
while read name empid dept
110
Linux 101 Hacks www.thegeekstuff.com
do
echo "$name is part of $dept department"
done
Assign execute privilege to the shell script and execute it.
$ chmod u+x read-employees.sh
$ ./read-employees.sh
Employee Names:
---------------
Emma Thomas is part of Marketing department
[ Pobierz całość w formacie PDF ]