








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









“A Shell Script is a text file that contains a sequence of commands to be executed by the shell.” It is used to automate repetitive tasks such as backups, software installation , and system administration. It is commonly written using Bash or sh.
Use the vi editor to create a file with .sh extension. vi myscript.sh
Press i to enter insert mode and type the required commands.
Press Esc , type :wq , and press Enter to save and exit.
Use the chmod command to make the script executable. chmod +x myscript.sh
Run the script using : ./myscript.sh
#!/bin/bash echo "Hello, World!" Save it as myscript.sh, give execute permission, and run it using ./myscript.sh.
fruit="apple" case $fruit in apple) echo "It's an apple" ;; banana) echo "It's a banana" ;; *) echo "Unknown fruit" ;; esac
The for loop is used to repeat commands for each item in a list. It processes each value one by one. It starts with for and ends with done. Example: for name in Ram Shyam; do echo "Hello $name" done Output : Hello Ram Hello Shyam
The while loop repeatedly executes commands as long as the condition is true. When the condition becomes false , the loop stops. It ends with done. Example: count= while [ $count -le 5 ]; do echo "Count: $count" ((count++)) done
If the command succeeds,! makes the result false; if it fails, ! makes the result true. Example: if! grep "hello" file.txt; then echo "Text not found" fi
Arithmetic operations in Shell Scripting are used to perform calculations such as addition, subtraction, multiplication, division, and modulus. In Linux shell scripting, arithmetic operations can be performed mainly using:
The expr command is an older method used to evaluate arithmetic expressions and return the result.
expr operand1 operator operand
a= b= result=expr $a + $b echo "Sum: $result"
Sum: 15
$(( )) is the recommended and modern method for performing arithmetic operations in Bash. It is simpler, cleaner, and easier to use than expr.
$(( expression ))
Filename expansion , also called wildcard expansion. It is a feature of the Linux shell used to match multiple files or directories without writing their complete names. The shell expands the wildcard pattern before executing the command. The main wildcards are *, ?, and [].
The * wildcard matches zero or more characters in a filename.
ls .txt Matches all files ending with .txt. ls a Matches all files starting with a. ls * Matches files and directories in the current directory.
The? wildcard matches exactly one character in a filename.
ls file?.txt It can match: file1.txt file2.txt fileA.txt
Another example: ls a?b.txt It can match filenames such as a1b.txt and acb.txt.
The [] wildcard matches one character from the specified set or range.
ls file[1-3].txt Matches: file1.txt file2.txt file3.txt Another example: ls file[a-c].txt Matches: filea.txt fileb.txt filec.txt
* → matches zero or more characters. ? → matches exactly one character. [] → matches one character from a set or range. Wildcards work with file and directory names , not with file contents.
Redirection in Linux shell is used to send the output of a command to a file or take input from a file instead of the keyboard. The main redirection symbols are >, >>, and <.
The > symbol is used to redirect the standard output (stdout) to a file. If the file already exists, its previous content is overwritten.
command > filename
echo "Hello" > output.txt This creates output.txt and writes Hello into it. If the file already contains data, the old content is replaced.
The >> symbol is used to append output to the end of a file. It does not remove the existing content of the file.
command >> filename
echo "New line" >> output.txt This adds new line at the end of output.txt while keeping the existing content.
The < symbol is used to take input from a file instead of the keyboard.
command < filename
wc -l < input.txt This takes the contents of input.txt as input to the wc -l command and counts the number of lines.