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 echo $SHELL Output: /bin/bash 2. View all shells supported by the system cat /etc/shells 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:
mkdir /usr/local/shelltest touch test.sh vim test.shExample script (saved as test.sh)
#!/bin/bash
echo "Hello World Shell"Run the script: bash test.sh Result: Hello World Shell The line #!/bin/bash (shebang) tells the system to use /bin/bash as the interpreter.
To execute a script directly you must grant execute permission: chmod +x test.sh Then run it with ./test.sh.
Shell Script Variables
Define a variable: name=zhiyikeji Use the variable with $:
echo $name echo ${name}Both produce zhiyikeji. Braces are optional but help avoid ambiguity.
Delete a variable: unset name After unsetting, echo $name outputs nothing.
Make a variable read‑only: readonly name Attempting to reassign a read‑only variable results in an error.
Shell Script Flow Control
If
#!/bin/bash
if [ $1 -gt 2 ]; then
echo "Value greater than 2"
else
echo "Value less than or equal to 2"
exit
fiRun with sh test2.sh 1 → Value less than or equal to 2; sh test2.sh 3 → Value greater than 2.
Case
case $1 in
start)
echo "start already begun"
;;
stop)
echo "stop command executed"
;;
esac
exitRunning sh service.sh start prints start already begun; sh service.sh stop prints stop command executed.
For Loop
j=$1
for ((i=1;i<=j;i++))
do
echo $i
doneExample: sh fortest.sh 6 outputs numbers 1 through 6.
While Loop
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 ""
doneProduces 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
