Under Development SHELL SCRIPTING Narasimha D Puneker
Audience & Pre-Requisite• Audience should be aware of what is linux OS• Should be able to boot and run commands• Should have linux booting before the session starts
Linux Shell• Shell is a user program or an environment provided for user interaction.• It is a command interpreter which reads from standard input / file and executes them• Types of Shell – BASH : Commonly used shell. – CSH : C Shell – syntax similar to C-Program – KSH : Korn Shell – From AT & T Bell Labs – TCSH : Compatible version of Berkeley UNIX C Shell• Type $cat /etc/shells : For available shell in the system
• Shell Script : Sequence of „n‟ number of commands that you store in a text file and execute the file, instead of entering commands.• This is similar to batch file fo MS-DOS
Shell script can take input from user, file and output them on screen. ShellScripts ?
Writing Scripts• Scripts are written using vi editor in command line or gedit in graphical mode• The script can be saved using extension “.sh”• The script can be saved without any extension, but it would be difficult to identify.•
Executing script Executing in bash shellType the following to see the output#bash <filename>Another shell (child shell) is created and the script is run and once the script is completed it exits from the childshell Execute by making executableChange the permission of script using command#chmod +x <filename>The execution takes place in the same shellType the following to see the output#./<filename>
Entry & Exit of a command when you enterEntry : a command on the command line and execute $? - return value 0 : indicatesExit : command successful $? – returns non-zero value : indicates an error
Read Input from• The read command takes input from keyboard and stores in a variable.• The value of the variable can be accessed by prefixing „$‟ sign to the variable.
Command & Arguments # <command> arg1 arg2 arg3<Command> Arg1 Arg2 ….. Arg9• $0 • $1 • $2 • $9Example:Open vi editor and type following, save and executeecho “Command Executed :” $0echo “Arguments entered :” $*echo “Argument 1 : “ $1echo “Argument 1:” $2 Execute by typing the command #bash <name of script> Hello World