Linux Shell Introduction

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    3 Favorites

    Linux Shell Introduction - Presentation Transcript

    1. Shell First Steps A jump start for programmers to start scripting. - Varun Torka, 2006EE10350
    2. Shell First Steps
      • Why scripting?
        • Because we are Lazy.
        • Because it is powerful, one of the main features in linux.
        • Because it is efficient and simple.
        • For the geekness of it, because it is cool.
        • Because we are Lazy.
    3. The Command Line
      • A Command Line is, in all simplicity, a user interface based on lines of commands. You can say that it is a textual direct serial processor. Most commonly, the user interacts directly with the computer by typing one line (although it can be more than one), which triggers actions from the computer based on the syntax of the current processor.(http://help.ubuntu.com)
        • $ command -option <argument>
    4. Inputting multiple commands
      • command1 ; command2
        • #one after other
      • command1 && command2
        • #command2 only if command returns true
      • command1 || command2
        • #command2 only if command1 returns false
    5. Control Flow
      • Redirection
        • command > filename
        • command >> filename #append
        • command < filename
      • Pipe
        • command | command
      • Wildcards
        • &quot;*&quot;, &quot;?&quot; and &quot;[x-y]&quot;
    6. Shell Programming
      • Example 1
      • #!/bin/bash
      • echo Hello World
        • This script has only two lines. The first indicates the system which program to use to run the file. The second line is the only action performed by this script, which prints 'Hello World' on the terminal.
    7. Shell Programming
      • Example 2
      • Variables:
      • #!/bin/bash
      • OF=/var/my-backup-$(date +%Y%m%d).tgz
      • tar -cZf $OF /home/me/
    8. Shell Programming
      • Example 3
      • Control Structures:
      • #!/bin/bash
      • T1=&quot;foo&quot;
      • T2=&quot;bar&quot;
      • if [ &quot;$T1&quot; = &quot;$T2&quot; ]; then # string comparison operator
      • echo expression evaluated as true
      • echo another line added
      • else
      • echo expression evaluated as false
      • fi
    9. Shell Programming
      • Example 4: Loop
      • #!/bin/bash
      • COUNTER=0
      • while [ $COUNTER -lt 10 ]; do # -lt = less than
      • echo The counter is $COUNTER
      • let COUNTER=COUNTER+1
      • done
    10. Shell Programming
      • Example 5: The For Loop
      • #!/bin/bash
      • for i in $( ls ); do
      • echo item: $i
      • done
      • #!/bin/bash
      • for i in `seq 1 10`;
      • do
      • echo $i
      • done
      The for loop is a little bit different from other programming languages. Basically, it let's you iterate over a series of 'words' within a string.
    11. Shell Programming
      • Example 6: Functions
      • #!/bin/bash
      • function quit {
      • exit
      • }
      • function e {
      • echo $1 #Parameter passing
      • }
      • e Hello
      • e World
      • quit
      • echo foo #Code will never reach here
        • Declaring a function is just a matter of writing function my_func { my_code }
    12. Shell Programming
      • Example 7
      #!/bin/bash if [ -z &quot;$1&quot; ]; then echo usage: $0 directory exit fi SRCD=$1 TGTD=&quot;/var/backups/&quot; OF=home-$(date +%Y%m%d).tgz tar -cZf $TGTD$OF $SRCD The expression in the first conditional tests if the program has received an argument ($1) and quits if it didn't, showing the user a little usage message. The rest of the script should be clear at this point.
    13. Shell Programming
      • Example 8:User Input
      #!/bin/bash echo Please, enter your firstname and lastname read FN LN echo &quot;Hi! $LN, $FN !&quot;
      • Example 9: Arithmetic Evaluation
      echo $((1+1)) #arithmetic operation echo $[1+1] # logical operation echo 3/4|bc -l # for printing fractions
    14. Shell Programming
      • Example 10
      Getting the return value of a program #!/bin/bash cd /dada &> /dev/null #/dada does not exist echo rv: $? Capturing a commands output #!/bin/bash DBS=`mysql -uroot -e&quot;show databases&quot;` for b in $DBS ; do mysql -uroot -e&quot;show tables from $b&quot; done
    15. Shell Programming
      • Revise...
      #!/bin/bash # renames.sh # basic file renamer criteria=$1 re_match=$2 replace=$3 for i in $( ls *$criteria* ); do src=$i tgt=$(echo $i | sed -e &quot;s/$re_match/$replace/&quot;) mv $src $tgt done
    16. Bibliography
      • https://help.ubuntu.com/community/CommandlineHowto
      • http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
      • Advanced shell scripting - http://tldp.org/LDP/abs/html/index.html
    17. Thank You and Keep Experimenting!!

    + Narendra SisodiyaNarendra Sisodiya, 2 years ago

    custom

    1212 views, 3 favs, 2 embeds more stats

    by Varun

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 1212
      • 1207 on SlideShare
      • 5 from embeds
    • Comments 0
    • Favorites 3
    • Downloads 84
    Most viewed embeds
    • 3 views on http://lug-iitd.org
    • 2 views on http://www.lug-iitd.org

    more

    All embeds
    • 3 views on http://lug-iitd.org
    • 2 views on http://www.lug-iitd.org

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories