Bash
Arithmetic operation using double parentheses construct in Bash
Oct 18th
The following script illustrates how double parentheses can be used to do arithmetic operations.
#!/bin/sh (( a = 23 )) echo "a (assign value) = $a" (( ++a )) echo "a (after ++a) = $a" (( --a )) echo "a (after --a) = $a" (( a++ )) echo "a (after a++) = $a" (( a-- )) echo "a (after a--) = $a" echo (( b = a>30?12:20 )) echo "If a > 30, then b = 12, else b = 20." echo "b = $b " echo
The following is output of the above script.
[neo@techpulp ~]# sh dpara.sh a (assign value) = More >
How to generate random/temporary file name using mcookie command
Oct 17th
The mcookie shell command is used to generate magic cookie for xauth. It throws a md5sum like output and the basic usage is as shown below.
[neo@techpulp ~]# mcookie 5f8dbb14e7eb4db75428017585115feb [neo@techpulp ~]#
Now let us use the output of mcookie command and form a random file name for temporary usage. In the following script, we use first characters of the random string printed by mcookie to form a random file name. The randfile() function takes prefix and suffix of the temporary file name and returns a temporary file name after checking for its existence.
#!/bin/bash
function randfile
{
while [ 1 ];
do
MCOUT=`mcookie`
TNAME="$1""${MCOUT:0:5}""$2"
if [ ! More > How to write infinite loop in Bash using for or while statement
Oct 17th
The while-do-done statement can be used to program an infinite loop in bash. The following sample shows how it can be done.
#!/bin/bash while [ 1 ]; do echo I am in infinite loop. Press Ctrl+C to stop me done
The break statement can be used to exit the while loop. The following sample code breaks away from while loop after iterating 10 times.
#!/bin/bash
i=1
while [ 1 ];
do
i=`expr $i + 1`
echo loop $i
if [ "$i" == "10" ]; then
echo Exiting while loop after iterating $i times
break
fi
done
echo end of shell script
The following will be the output More >
How to generate temporary file name in bash using RANDOM environment variable
Oct 17th
Whenever the $RANDOM environment variable is accessed, it gives a random number. This can be used to generate a random file name. In the following example randfile function takes prefix and suffix of the temporary file name and returns a temporary file name after checking for its existence.
#!/bin/bash
function randfile
{
while [ 1 ];
do
TNAME="$1""$RANDOM""$2"
if [ ! -f $TNAME ]; then
echo $TNAME
exit 0
fi
done
}
NAME=`randfile /tmp/test- .tmp`
echo $NAME
How to access command line arguments in a bash script
Oct 9th
The following example script illustrates how command line arguments passed to a bash shell script can be accessed. A special variable $0 contains name of the command and $# contains number of command line arguments passed to the script. Actual arguments can be accessed using the argument number prefixed with $ sign. i.e The first argument is accessed using $1 and $2 to access second argument so on and so forth.
[neo@techpulp ~]# cat cmdargs.sh #!/bin/bash echo Name of script: $0 echo First argument: $1 echo Second argument: $2 echo Number of arguments: $# echo All arguments: $@ [neo@techpulp ~]# [neo@techpulp ~]# sh cmdargs.sh arg1 arg2 More >
Export a bash function as command
Oct 4th
You can use a function written in bash script to use like any other Unix/Linux command. This function can take command line arguments and return a value as exit code similar to standard commands. In the following example, we try to identify if a directory exists with the given name. The function returns 1 if a directory exists. Otherwise returns 0. You can copy the following piece of code and keep in a script or paste it on the console.
function isdir()
{
echo REQ isdir $1
if [ -d "$1" ]; then
return 1
else
return 0
fi
}
export More > 

Recent Comments