Master Shell Scripting: From Basics to Advanced Control Structures
This tutorial walks Java developers through the fundamentals of Linux shell scripting, covering how to create and run scripts, check and set the default shell, manage permissions, define and manipulate variables, and implement flow‑control constructs such as if/else, case, for, and while loops with practical code examples.
Java developers often work on Linux servers, packaging projects as JAR or WAR files, uploading them via XFTP, and starting them with a service script; now they can learn how to write their own shell scripts.
Shell Script
A shell script is a program file that contains a series of commands interpreted by a command‑line interpreter (the shell). When commands are placed in a file instead of typed interactively, the file becomes a shell script.
Shell scripts can include assignments, calculations, loops, and other operations.
1. View the current default shell
<code>echo $SHELL</code>Output:
/bin/bash2. View all shells supported by the system
<code>cat /etc/shells</code>Output:
/bin/sh /bin/bash /usr/bin/sh /usr/bin/bashHow to Write a Shell Script
Create a directory and a script file, then edit it:
<code>mkdir /usr/local/shelltest</code> <code>touch test.sh</code> <code>vim test.sh</code>Example script (saved as
test.sh)
<code>#!/bin/bash
echo "Hello World Shell"
</code>Run the script:
<code>bash test.sh</code>Result:
Hello World ShellThe line
#!/bin/bash(shebang) tells the system to use
/bin/bashas the interpreter.
To execute a script directly you must grant execute permission:
<code>chmod +x test.sh</code>Then run it with
./test.sh.
Shell Script Variables
Define a variable:
<code>name=zhiyikeji</code>Use the variable with
$:
<code>echo $name</code> <code>echo ${name}</code>Both produce
zhiyikeji. Braces are optional but help avoid ambiguity.
Delete a variable:
<code>unset name</code>After unsetting,
echo $nameoutputs nothing.
Make a variable read‑only:
<code>readonly name</code>Attempting to reassign a read‑only variable results in an error.
Shell Script Flow Control
If
<code>#!/bin/bash
if [ $1 -gt 2 ]; then
echo "Value greater than 2"
else
echo "Value less than or equal to 2"
exit
fi
</code>Run with
sh test2.sh 1→
Value less than or equal to 2;
sh test2.sh 3→
Value greater than 2.
Case
<code>case $1 in
start)
echo "start already begun"
;;
stop)
echo "stop command executed"
;;
esac
exit
</code>Running
sh service.sh startprints
start already begun;
sh service.sh stopprints
stop command executed.
For Loop
<code>j=$1
for ((i=1;i<=j;i++))
do
echo $i
done
</code>Example:
sh fortest.sh 6outputs numbers 1 through 6.
While Loop
<code>a=1
b=1
while ((a <= 9))
do
while ((b <= a))
do
let "c=a*b"
echo -n "$a*$b=$c "
let b++
done
let a++
let b=1
echo ""
done
</code>Produces the classic 9×9 multiplication table.
Shell scripting is a practical skill for automating tasks on Linux servers; mastering these basics enables you to write simple automation scripts and lay the groundwork for more complex DevOps workflows.
macrozheng
Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.