Write For Us

Linux Tutorial For Beginners 13 Unix Shell Scripting-Introduction Part-I

E-Commerce Solutions SEO Solutions Marketing Solutions
294 Views
Published
RHEL 6 Session 9 Bash Shell Scripting Part 1 Introduction to Shell Scripting. To Learn or Teach Linux visit www.theskillpedia.com, A Marketplace for Learners and Trainers. For Corporate Training Requirements Visit My Website www.rnsangwan.com

Slides Contents
Bash Shell Scripting
R N Sangwan
Scripting Basics
Shell scripts are text files that contains a series of commands or statements to be executed.
Shell scripts are useful for:
Automating commonly used commands.
Performing system administration and troubleshooting
Creating simple applications
Manipulations of text or files.
Application prototyping
Creating Shell Scripts
Step 1: Use a text editor such as vi to create a text file containing commnds.
First line contains the magic “shbang” sequence : #!
#!/bin/bash
Comment your scripts
Comments start with a #
Create shell script which is self documenting.
If you enter this by pressing \ key followed by the Enter key on the most keyboards. This will enable you to enter one command that spans multiple lines.
Creating Shell Scripts cont.
Step 2: Make the script executable
$ chmod a+x myscript.sh
To execute the new script:
Place the script file in a directory in the executable path –OR-
Specify the absolute path or relative path to the script on the command line.
Generating Output
Use echo to generate simple output
$ echo ‘Welcome to Redhat Linux paradise!’
$ echo –n “please enter the file name: “
Use printf to generate formatted output.
$ printf “the result is %0.2f\n” $RESULT
Syntax similar to C printf function
Does not automatically put a newline at the end of the output.
Handling Input
Use read to assign an input value to a shell variable:
echo –n “Enter the filename: “
read FILENAME
read reads from standard input and assigns one word to each variable.
Any leftover words are assigned to the last variable.
A word is defined as a character string surrounding with white space such as spaces and tabs.
Can be changed by IFS. IFS=‘:’
If there are more words than variables, the last variable is assigned all the remaining words.
bash scripts example
#!/bin/bash
echo –n ‘Enter name First Last:’
read FIRST LAST
printf “Your First name is %s and your last name is %s \n” \ $FIRST $LAST
The -p option is used to display a prompt string. Place quotes around the string if you need to prompt the user with a multiple-word command
#!/bin/bash
read –p “Enter Several Values:” value1 value2 value3
echo “value 1 is $value1”
echo “value 2 is $value2”
echo “value 3 is $value3”


Exit Status
Commands exit with an exit status
0 for success, 1 to 255 for failure
Exit status of most recently executed command is kept in the $? Variable just like return values form shell functions.
Shell scripts may set an exit status with the exit command.
exit 1 # indicated an error
Category
Tech
Sign in or sign up to post comments.
Be the first to comment