First & foremost to create sub-shells within a script you need to use round brackets "( )". Value assigned to outermost shell is always 0. As you move inside sub-shells their values keep on increasing by 1 as depicted in the example shown below:
#!/bin/bash
#Author: Parag Kalra
#Date: 31st May, 2008
#Name: subshell1.sh
#Usage: ./subshell1.sh
#Purpose: To illustrate values assigned to sub-shells
echo "We are outside the subshell."
echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
(
echo "We are inside first subshell."
echo "Subshell level INSIDE first subshell = $BASH_SUBSHELL"
(
echo "Inside second sub-shell"
echo "Subshell level INSIDE second subshell = $BASH_SUBSHELL"
(
echo "Inside third sub-shell"
echo "Subshell level INSIDE third subshell = $BASH_SUBSHELL"
)
)
)
echo "We are again outside all the subshells."
echo "Subshell level OUTSIDE subshell = $BASH_SUBSHELL"
#END OF SCRIPT
The output of above script is:
We are outside the subshell.
Subshell level OUTSIDE subshell = 0
We are inside first subshell.
Subshell level INSIDE first subshell = 1
Inside second sub-shell
Subshell level INSIDE second subshell = 2
Inside third sub-shell
Subshell level INSIDE third subshell = 3
We are again outside all the subshells.
Subshell level OUTSIDE subshell = 0
Friday, May 30, 2008
Thursday, May 29, 2008
Shell-Script to add headers to another Shell-Script
I write Shell-Scripts day in day out. For each and every shell-script I need to place following headers at the beginning of it. For e.g. consider following header:
#!/bin/bash
#Author:
#Date:
#Name:
#Usage:
#Purpose:
I have prepared a shell-script to automatically add the above header to the newly created shell-script. Just go through it and let me know if you happen to come across any bug in it:
#!/bin/bash
#Author: Parag Kalra
#Date: 30th May, 2008
#Name: shell-script-header.sh
#Usage: ./shell-script-header.sh
#Purpose: To add headers to shell-scripts
echo "Enter the date: "
read SHDATE
echo "Enter the name of the shell script: "
read SHNAME
echo "Enter the purpose of this shell script: "
read SHPURPOSE
export SHDATE #='$SHDATE'
export SHNAME #='$SHNAME'
export SHPURPOSE #='$SHPURPOSE'
touch $SHNAME
echo "#!/bin/bash" >> $SHNAME
echo "#Author: Parag Kalra" >> $SHNAME
echo "#Date: $SHDATE" >> $SHNAME
echo "#Name: $SHNAME" >> $SHNAME
echo "#Usage: ./$SHNAME" >> $SHNAME
echo "#Purpose: $SHPURPOSE" >> $SHNAME
#END OF SCRIPT
#!/bin/bash
#Author:
#Date:
#Name:
#Usage:
#Purpose:
I have prepared a shell-script to automatically add the above header to the newly created shell-script. Just go through it and let me know if you happen to come across any bug in it:
#!/bin/bash
#Author: Parag Kalra
#Date: 30th May, 2008
#Name: shell-script-header.sh
#Usage: ./shell-script-header.sh
#Purpose: To add headers to shell-scripts
echo "Enter the date: "
read SHDATE
echo "Enter the name of the shell script: "
read SHNAME
echo "Enter the purpose of this shell script: "
read SHPURPOSE
export SHDATE #='$SHDATE'
export SHNAME #='$SHNAME'
export SHPURPOSE #='$SHPURPOSE'
touch $SHNAME
echo "#!/bin/bash" >> $SHNAME
echo "#Author: Parag Kalra" >> $SHNAME
echo "#Date: $SHDATE" >> $SHNAME
echo "#Name: $SHNAME" >> $SHNAME
echo "#Usage: ./$SHNAME" >> $SHNAME
echo "#Purpose: $SHPURPOSE" >> $SHNAME
#END OF SCRIPT
Subscribe to:
Comments (Atom)
