Introduction to Unix Shell Scripting Basics with Prof. Mahesh G. Huddar

unix shell scripting basics n.w
1 / 68
Embed
Share

"Explore the fundamentals of Unix shell scripting with Assistant Professor Mahesh G. Huddar at HIT, Nidasoshi. Learn about different shells, creating shell scripts, executing commands, and more in this comprehensive guide."

  • Unix Shell Scripting
  • Basics
  • Mahesh G. Huddar
  • Assistant Professor
  • HIT

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. UNIX Shell-Scripting Basics Mahesh G. Huddar Asst. Professor HIT, Nidasoshi

  2. Agenda What is a shell? A shell script? Introduction to bash Running Commands Applied Shell Programming

  3. What is a shell?

  4. What is a shell? /bin/bash

  5. What is a shell? #!/bin/bash

  6. What is a shell? INPUT shell OUTPUT ERROR

  7. What is a shell? Any Program But there are a few popular shells

  8. Bourne Shells /bin/sh /bin/bash Bourne-Again Shell Steve Bourne

  9. Other Common Shells C Shell (/bin/csh) Turbo C Shell (/bin/tcsh) Korn Shell (/bin/ksh)

  10. An aside: What do I mean by /bin ? C Shell (/bin/csh) Turbo C Shell (/bin/tcsh) Korn Shell (/bin/ksh)

  11. An aside: What do I mean by /bin ? /bin, /usr/bin, /usr/local/bin /sbin, /usr/sbin, /usr/local/sbin /tmp /dev /home/borwicjh

  12. What is a Shell Script? A Text File With Instructions Executable

  13. What is a Shell Script? % cat > hello.sh <<MY_PROGRAM #!/bin/sh echo Hello, world MY_PROGRAM % chmod +x hello.sh % ./hello.sh Hello, world

  14. What is a Shell Script? A Text File % cat > hello.sh <<MY_PROGRAM % cat > hello.sh <<MY_PROGRAM #!/bin/sh echo Hello, world MY_PROGRAM MY_PROGRAM % chmod +x hello.sh % ./hello.sh Hello, world

  15. An aside: Redirection cat > /tmp/myfile cat >> /tmp/myfile cat 2> /tmp/myerr cat < /tmp/myinput cat <<INPUT Some input INPUT cat > /tmp/x 2>&1 INPUT 0 env OUTPUT ERROR 1 2

  16. What is a Shell Script? How To Run % cat > hello.sh <<MY_PROGRAM #!/bin/sh #!/bin/sh echo Hello, world MY_PROGRAM % chmod +x hello.sh % ./hello.sh Hello, world

  17. What is a Shell Script? What To Do % cat > hello.sh <<MY_PROGRAM #!/bin/sh echo Hello, world echo Hello, world MY_PROGRAM % chmod +x hello.sh % ./hello.sh Hello, world

  18. What is a Shell Script? Executable % cat > hello.sh <<MY_PROGRAM #!/bin/sh echo Hello, world MY_PROGRAM % chmod +x hello.sh % chmod +x hello.sh % ./hello.sh Hello, world

  19. What is a Shell Script? Running it % cat > hello.sh <<MY_PROGRAM #!/bin/sh echo Hello, world MY_PROGRAM % chmod +x hello.sh % ./hello.sh % ./hello.sh Hello, world Hello, world

  20. Finding the program: PATH % ./hello.sh echo vs. /usr/bin/echo % echo $PATH /bin:/usr/bin:/usr/local/bin: /home/borwicjh/bin % which echo /usr/bin/echo

  21. Variables and the Environment % hello.sh bash: hello.sh: Command not found % PATH= $PATH:. % hello.sh Hello, world

  22. An aside: Quoting % echo $USER $USER % echo $USER borwicjh % echo \ % echo deacnet\\sct deacnet\sct % echo \ \

  23. Variables and the Environment % env [ variables passed to sub-programs ] % NEW_VAR= Yes % echo $NEW_VAR Yes % env [ PATH but not NEW_VAR ] % export NEW_VAR % env [ PATH and NEW_VAR ]

  24. How to Learn man man bash man cat man man man k man k manual Learning the Bash Shell, 2nd Ed. Bash Reference Cards http://www.tldp.org/LDP/abs/html/

  25. Introduction to bash

  26. Continuing Lines: \ % echo This \ Is \ A \ Very \ Long \ Command Line This Is A Very Long Command Line %

  27. Exit Status $? 0 is True % ls /does/not/exist % echo $? 1 % echo $? 0

  28. Exit Status: exit % cat > test.sh <<_TEST_ exit 3 _TEST_ % chmod +x test.sh % ./test.sh % echo $? 3

  29. Logic: test % test 1 -lt 10 % echo $? 0 % test 1 == 10 % echo $? 1

  30. Logic: test test [ ] [ 1 lt 10 ] [[ ]] [[ this string =~ this ]] (( )) (( 1 < 10 ))

  31. Logic: test [ -f /etc/passwd ] [ ! f /etc/passwd ] [ -f /etc/passwd a f /etc/shadow ] [ -f /etc/passwd o f /etc/shadow ]

  32. An aside: $(( )) for Math % echo $(( 1 + 2 )) 3 % echo $(( 2 * 3 )) 6 % echo $(( 1 / 3 )) 0

  33. Logic: if if something then : # elif a contraction of else if : elif something-else then : else then : fi

  34. Logic: if if [ $USER eq borwicjh ] then : # elif a contraction of else if : elif ls /etc/oratab then : else then : fi

  35. Logic: if # see if a file exists if [ -e /etc/passwd ] then echo /etc/passwd exists else echo /etc/passwd not found! fi

  36. Logic: for for i in 1 2 3 do echo $i done

  37. Logic: for for i in /* do echo Listing $i: ls -l $i read done

  38. Logic: for for i in /* do echo Listing $i: ls -l $i read done

  39. Logic: for for i in /* do echo Listing $i: ls -l $i read done

  40. Logic: C-style for for (( expr1 ; expr2 ; expr3 )) do list done

  41. Logic: C-style for LIMIT=10 for (( a=1 ; a<=LIMIT ; a++ )) do echo n $a done

  42. Logic: while while something do : done

  43. Logic: while a=0; LIMIT=10 while [ "$a" -lt "$LIMIT" ] do echo -n "$a a=$(( a + 1 )) done

  44. Counters COUNTER=0 while [ -e $FILE.COUNTER ] do COUNTER=$(( COUNTER + 1)) done Note: race condition

  45. Reusing Code: Sourcing % cat > /path/to/my/passwords <<_PW_ FTP_USER= sct _PW_ % echo $FTP_USER % . . /path/to/my/passwords % echo $FTP_USER sct %

  46. Variable Manipulation % FILEPATH=/path/to/my/output.lis % echo $FILEPATH /path/to/my/output.lis % echo ${FILEPATH%.lis} /path/to/my/output % echo ${FILEPATH#*/} path/to/my/output.lis % echo ${FILEPATH##*/} output.lis

  47. It takes a long time to become a bash guru

  48. Running Programs

  49. Reasons for Running Programs Check Return Code $? Get Job Output OUTPUT=`echo Hello ` OUTPUT=$(echo Hello ) Send Output Somewhere Redirection: <, > Pipes

  50. Pipes INPUT 0 Lots of Little Tools echo OUTPUT ERROR 1 2 echo Hello | \ wc -c A Pipe! INPUT 0 wc OUTPUT ERROR 1 2

More Related Content