Shell scripting

5,051 views

Published on

Published in: Education, Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,051
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
231
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Shell scripting

  1. 1. Under Development SHELL SCRIPTING Narasimha D Puneker
  2. 2. 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
  3. 3. 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
  4. 4. • 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
  5. 5. Shell script can take input from user, file and output them on screen. ShellScripts ?
  6. 6. 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.•
  7. 7. 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>
  8. 8. 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
  9. 9. 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.
  10. 10. 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
  11. 11. Input Output Redirection Output Input Redirection Redirection > >> < Example: Example: Example: Command >> Command < Command > filename filename (Takes filename (creates (appends existing input from file new file) else creates new) instead of keyboard)
  12. 12. Example#cat snameVivekMaheshAshishZebraBabu(Press Ctrl+D to save)#cat sort < sname > sorted_names#tr “[a-z]” “[A-Z]” < sname > cap_names#sort > new_sorted < sname#cat new_sorted

×