SlideServe
Menu
Préférences
© SlideServe · v2026

Shell script – part 2

tyrone avatar
18 diapositives · 402 Views · Jul 10, 2014

Shell script – part 2. CS 302. Special shell variable $0 .. $9. Positional parameters or command line arguments For example , a script myscript take 2 arguments , foo and bar Using special variable, you can access your script and any parameters it has. myscript $0 foo $1 Bar $2.

Shell script – part 2 - slide 1
01 / 18Shell script – part 2
1 / 17

Télécharger la présentation

Shell script – part 2
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Shell script – part 2 CS 302

  2. Special shell variable $0 .. $9 • Positional parameters or command line arguments • For example , a script myscript take 2 arguments , foo and bar • Using special variable, you can access your script and any parameters it has. myscript $0 foo$1 Bar$2 $#tells you how many parameter your script was given

  3. If condition • if condition is used for decision making in shell script, If given condition is true then command1 is executed. • Syntax: • if condition • then • command1... • fi

  4. test expression or [ expr ] Is used to see if an expression is true Syntax: test expression OR [ expression ] • Run it as follows: • sh script.txt 3

  5. test expression or [ expr ]

  6. test expression or [ expr ]

  7. test expression or [ expr ]

  8. If .. else .. fi • If given condition is true then command1 is executed otherwise command2 is executed. • Syntax: • if condition • then • command1 • else • if condition is not true then execute all commands up to fi • fi

  9. Example (1) if [ $# -eq 0 ] then echo " Enter one argument “ exit 1 else echo " The first argument is $1 " fi

  10. Class exercise (1) • Write a Script to see whether argument is positive or negative. First, you should ensure that a user enter an argument , if not exist and print this message “You must give/supply one integers”

  11. # Script to see whether argument is positive or negativeif [ $# -eq 0 ]thenecho “You must give/supply one integer"exit 1fi if test $1 -ge 0thenecho "$1 number is positive"elseecho "$1 number is negative"fi

  12. Nested if-else-fi

  13. Multilevel if-then-else Syntax: if condition then condition is zero (true - 0) execute all commands up to elif statement elif condition1 then condition1 is zero (true - 0) execute all commands up to elif statement elif condition2 then condition2 is zero (true - 0) execute all commands up to elif statement else None of the above condtion,condtion1,condtion2 are true (i.e. all of the above nonzero or false) execute all commands up to fi fi

  14. Example :

  15. For loop Syntax: for { variable name } in { list } Do execute one for each item in the list until the list is not finished done or: for ((initial vale ; condition ;)) Do execute one for each item in the list until the list is not finished done

  16. Example

  17. While loop Syntax: while [ condition ] do command1 command2 command3 .. .... done

  18. Example