Shell Script Examples
Shell Script is a powerful program designed to be run by the Unix-like operating system. They are often used to manipulate application servers such as starting, stoping and restarting servers or to prepare deployment scripts including backup, checking, patch and rollback. In this article, I have included some shell script examples which should be sufficient for Java Developers. Of course, the most useful command is man
which stands for manual page. It provides detailed infomation of non built-in commands.
The first line of shell script is defined as follows:
#!/bin/sh
This is called a shebang
and please check this for more information.
define variables #
Defining variables in shell script is simple, they are defined as name=value
and no spaces allowed.
isDebugEnabled=true
java_version=17
domain="caizhenhua.me"
subdomain="www"."$domain"
echo $isDebugEnabled
echo $java_version
echo $domain
echo $subdomain
Output:
true
17
caizhenhua.me
www.caizhenhua.me
backtick #
The commands inside the backtick
is executed first before the main command.
echo pwd
echo `pwd`
Output:
pwd
/Users/caizhenhua
case #
The case
builtin command in shell script works similar to the Java Switch Statements.
echo "Enter a number:\n1 : current time\n2 : current date"
read INPUT_STRING
case $INPUT_STRING in
1)
echo $(date +"%r")
;;
2)
echo $(date +'%d/%m/%Y')
;;
*)
echo $(date)
;;
esac
Output:
Enter a number:
1 : current time
2 : current date
1
11:39:14 PM
basename #
basename
command can help to strip directory or suffix of filename.
echo `basename /Users/caizhenhua/tomcat/bin/catalina.sh`
echo `basename -s .sh /Users/caizhenhua/tomcat/bin/catalina.sh`
Output:
catalina.sh
catalina
dirname #
dirname
command works similar to the basename
command except that it deletes the filename portion.
echo `dirname /Users/caizhenhua/tomcat/bin/catalina.sh`
Output:
/Users/caizhenhua/tomcat/bin
$0 #
This $0
command represents the name of the script.
echo $0
echo `basename $0`
echo `basename -s .sh $0`
Output:
$ ./scriptname.sh
./scriptname.sh
scriptname.sh
scriptname
$1~N #
The positional parameters $1
, $2
, $3
etc., are the arguments passed to a shell script.
echo first param is: $1
echo second param is: $2
echo third param is: $3
Output:
./pparam.sh one two three
first param is: one
second param is: two
third param is: three
$@ #
The $@
command stores all passing arguments in a list of string.
./expandpp.sh one two three
echo "$@"
array=("$@")
echo ${array[1]}
Output:
one two three
two
while #
The while
loop allows commands to be executed repeatedly.
x=10
while [ $x -gt 0 ]
do
echo "Happy New Year in $x Seconds"
x=$(( $x - 1 ))
done
echo "HAPPY NEW YEAR !!!"
Output:
Happy New Year in 10 Seconds
Happy New Year in 9 Seconds
Happy New Year in 8 Seconds
Happy New Year in 7 Seconds
Happy New Year in 6 Seconds
Happy New Year in 5 Seconds
Happy New Year in 4 Seconds
Happy New Year in 3 Seconds
Happy New Year in 2 Seconds
Happy New Year in 1 Seconds
HAPPY NEW YEAR !!!
if #
The if
command is a control flow statement that only executes code when the conditions satisfy.
# spaces between square brackets are necessary
if [ -e while.sh ]
then
echo "while.sh file exists"
else
echo "while.sh file not found"
fi
Output:
while.sh file exists
expr #
Expression evaluation or expr
calculates the expression and write the results on the standard output.
echo `expr 12 + 4`
Output:
16
[ #
This command [
is called test
and it is used for testing expression and return either true or false.
if [ -e while.sh ] # if a file exist
if [ -r while.sh ] # if a file exist and is readable
if [ "$a" -eq "$b" ] # if number a is equal to number b
if [ "$a" = "$b" ] # if string a is equal to string b
. #
A dot .
in shell script means to copy or to source
the contents of that file into the current shell.
. ./setjavahome.sh
echo $JAVA_HOME
And the setjavahome.sh file:
JAVA_HOME=/usr/czh/java17/bin
Output:
/usr/czh/java17/bin
export #
The export
command is similar to the source
command such that the variable assignments are visible to the subprocesses. The assignment does not affect the variable in the parent shell. This is particular useful when you have multiple Java applications running on your system with different JDK versions.
echo $PATH
JAVA_HOME=/usr/czh/java17/bin
export PATH=$JAVA_HOME/bin:$PATH
echo $PATH
Output:
/Users/caizhenhua/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
/usr/czh/java17/bin/bin:/Users/caizhenhua/.rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
umask #
The umask
command is used to set the default access for a new file.
echo `umask`
echo `umask -S`
umask u-w
echo `umask`
umask u+w
echo `umask`
Output:
0022
u=rwx,g=rx,o=rx
0222
0022
unset #
The unset
command is uset to delete the shell variable or function.
java_version=17
echo $java_version
unset java_version
echo $java_version
Output:
17
shift #
The shift
command is used to move the positional parameters to one position left.
echo first param is: $1
echo second param is: $2
echo third param is: $3
shift
echo first param is: $1
echo second param is: $2
echo third param is: $3
Output:
./pparam.sh one two three
first param is: one
second param is: two
third param is: three
first param is: two
second param is: three
third param is:
> #
The shell script interprets the redirection opeartor >
as standard output redirection such that the previous contents are copied to another file.
ps aux > process.log
The process.log
file will be overriden if it was already exist. The >>
opeartor works similar to >
except it appends the previous content instead of overriding.
2>&1 #
File descriptor 2
is the standard error (stderr
) and file descriptor 1
is the standard output. &
tells the shell to treat 1
as file descriptor and not a filename. In short, 2>&1
means redirect the standard error to the same place as the standard output.
cat file-does-not-exist > test1.log
cat file-does-not-exist > test2.log 2>&1
The test1.log
file does not contain the error message cat: file-does-not-exist: No such file or directory
while test2.log
does.
/dev/null #
/dev/null
is a special file called the null device and it discards eveything written into it.
cat file-does-not-exist > /dev/null 2>&1
& at the end #
If we put &
at the end of a command, it simply means run this command in the background.
cat file-does-not-exist > /dev/null 2>&1 &
- Previous: Common Linux Commands
- Next: Java FileInputStream Example