Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Shell Script

Shell scripts allow users to automate tasks by writing series of commands in a plain text file. There are several benefits to writing shell scripts including taking input from users or files, creating custom commands to save time, and automating system administration and everyday tasks. Scripts can be created using a text editor by adding commands and making the file executable. Conditionals like if/else and case allow scripts to make decisions based on different conditions.

Uploaded by

Karan Chavda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Shell Script

Shell scripts allow users to automate tasks by writing series of commands in a plain text file. There are several benefits to writing shell scripts including taking input from users or files, creating custom commands to save time, and automating system administration and everyday tasks. Scripts can be created using a text editor by adding commands and making the file executable. Conditionals like if/else and case allow scripts to make decisions based on different conditions.

Uploaded by

Karan Chavda
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 3

Shell Script : A file which contains a group of commands that has to be executed ,such a file is called shell scripts,

shell programs or shell procedures. There is no restriction on the extension of the filename, though it is a universal convention that the extension .sh be used for shell programs. Shell script defined as : "Shell Script is series of command written in plain text file. Shell script is just like batch file is MS-DOS but have more power than the MS-DOS batch file."

Why to Write Shell Script ? Shell script can take input from user, file and output them on screen. Useful to create our own commands. Save lots of time. To automate some task of day today life. System Administration part can be also automated.

Starting with scripts: Steps to create shell script 1. vi script1.sh 2. press i or I 3. type list of commands eg. #first shell script date cal <ESC>:wq 4. execute file 1. sh script.sh 2. chmod +x script.sh or chmod 755 script.sh script.sh Using if 1 if condition is true then execute commands fi

if condition is true then execute commands else execute commands fi if [ condition ] ; then execute statements elif [ condition ] ; then execute statements else execute statements fi

Case conditional case expression in pattern1) pattern2) *) esac execute cmds ;; execute cmds ;; ... ... execute cmds

eg. case "$choice" in 1) ls ;; 2) date ;; 3) who ;; 4) exit esac

Relational operators used by if Meaning Equal to Not equal to Greater than Greater than or equal to Less than Less than or equal to

Operator -eq -ne -gt -ge -lt -le

String Tests used by test Exit Status True if string stg is not a null string True if string stg is a null string True if string s1 = s2 True if string s1 is not equal to s2 True if string stg is assigned and not null

Test -n stg-z stg s1 = s2 s1 != s2 stg

File related Tests with test Test -e file -f file-r file-w file -x file -d file -s fileExit Status True if file exists True if file exists and is a regular file True if file exists and is a readable file True if file exists and is a writable file True if file exists and is a executable file True if file exists and is a directory file True if file exists and has a size greater than zero

while Loop while condition is true do execute commands done for loop for variable in list do execute commands done

for (( exp1;exp2;exp3 )) { execute commands }

You might also like