Linux Shell Scripting Beginner Tutorial – Part 1
- Last Updated On: August 8, 2016
- By: Scriptcrunch Editorial
Shell script file name has a .sh extension.
How to run a shell script:
1. Create a fileLinux shell is an interactive mode to run the Linux commands. You can type commands in terminal using keyboard and you can run it using shell. Lets say , you want to automate some action on your Linux machine. In this case you cannot feed the terminal with commands one by one. So what you can do is, save the commands in a file and execute it. So that all the commands in the file will be executed one by one automatically. This is called shell script.
Shell script file name has a .sh extension.
How to run a shell script:
1. Create a file
vi demo.sh
2. Enter any basic command
echo “ this is a demo shell script by $USER”
Here $USER will display the current user name who is logged in. $USER is called environment variable. Ther are few system defined variables , which is associated with a system value. Eg: $HOME, $USERNAME , $OSTYPE etc..
You can list the environment variables by using the following command in the terminal.
env
3. Save the file and change the read write permissions to 755
chmod 755 demo.sh
4. Execute the script using any one of the following method.
./demo.sh Bash demo.sh .demo.sh
5. Once executed, you will get the output mentioned in the echo section of the script.
How to know if the script has executed successfully:
Shell script is associated with exit codes 1 , 0, 127 etc,,. If the script is executed successfully, it will give exit code 0. If not it will give a non-zero exit code . Exit code 1 means there are many possible errors. Exit code 127 means “command not found”. You can check the exit code of previously executed command using the following command.
echo $?
Try typing valid and invalid commands in the terminal and check the exit code status using the above command.
Working with variables:
Variables are used to store values in Linux like any programming language. You can assign any integer , float , string etc,, values to a variable. When you assign a value to a variable , there should not be any space on both sides of the “=” sign. Also, variables are case sensitive. A and a are considered as two different variables.
You can assign a value to a variable as shown below.
a=5 name=scriptscrunch b=1.2
sample shell script to add two numbers using varibales:
save the following script in a file with .sh extension and run it in the terminal to see the output.
a=1 b=2 c=`expr $a + $b` echo $c
In the above sample script, values 1 and 2 are assigned to variables a and b. Variable c is assigned to evaluate a match expression. Here “expr “ has to be used for mathematical expressions, otherwise the shell will take the assigned values as string.
When using expr, there should be a space on both sides of the arithmetic operator .
Adding two numbers by taking input from the user:
echo " Sample addition of two numbers" echo "enter the first number" read a echo "enter the second number" read b c=`expr $a + $b` echo $c “echo $c” echoes out the output value stored in variable c.
read command is used to get the input from the keyboard. In the above script, read gets the input from the user and stores it in a variable. The values stored in the variables are added using the expr function.
About echo command:
1. if you want to use escape characters like n or t , you have to execute the echo command using “-e” switch.
Eg: echo –e “this t words t in t this t sentence t are t separated t by tabs”
2. you can execute commands inside an echo statement by using single quotes.
echo "this is echo inside `echo "echo statement" ’ "
Running multiple commands in a line:
To run multiple commands on a line, separate the commands using semi colon (;)
ls ; echo "demo" ; date
A simple command to sort names in a file:
Lets say you have 100 names in a file and you want to sort it in alphabetical order. The following command will do the job.
sort < names > sorted_names
the sort function sorts the name by getting the names from the file (names) using < operator. Once sorted , it will output the results to a new file (sorted_names) you mention in the command using > operator.
Kindly leave your feedback in the comments section below, so that I can improve the tutorial series.
Scriptcrunch Editorial
Other Interesting Blogs
[40% OFF] Linux Foundation LFCA, LFCS & LFCT Exam Voucher Codes
Linux Foundation has announced up to a $284 discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux
Linux Foundation Coupon for October 2024
Hi Techies, I wanted to let you know about a pretty sweet deal with the Linux Foundation Coupon that is running now.
CKA Certification Study Guide (Certified Kubernetes Administrator)
This comprehensive CKA certification exam study guide covers all the important aspects of the Certified Kubernetes Administrator exam and useful resources. Passing