1 / 12

Linux Shell Script

Linux Shell Script. Shell script. The first line is used to specify shell program #!/bin/ sh Variables variable=“text” variable=0 variable =`program arguments` To use the declared variable s=“hello” echo $ s echo ${s}_123 System environment variables $ export.

tyme
Download Presentation

Linux Shell Script

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linux Shell Script

  2. Shell script • The first line is used to specify shell program • #!/bin/sh • Variables variable=“text” variable=0 variable=`program arguments` • To use the declared variable s=“hello” echo $s echo ${s}_123 • System environment variables $ export

  3. Passing argument • Special variables • Number of arguments $# • 1st, 2nd, .. arguments $1, $2, ... • Each argument $@ = .$1. .$2.... • All arguments $* = .$1 $2 .... • Exit status $?

  4. echo - display text or variable to stdout • echo “hello” • read - read from stdin read s echo “you type: $s” • expr- evaluate the expression read i j=`expr $i + 10` echo $j

  5. Shell Arithmetic • Use to perform arithmetic operations. • Syntax:expr op1 math-operator op2Examples: $ expr 1 + 3$ expr 2 - 1$ expr 10 \/ 2$ expr 20 \% 3$ expr 10 \* 3$ echo `expr 6 + 3`

  6. if .- conditional statement if condition then ... fi if [ “$s” = “abc” ]; then ... fi • Most conditions can be written by using test or [...] • Test if file size > 0 -s filename • Test if file exists -f filename • Test file permission -r -w -x filename • String equality string1 = string2 • String inequality string1 != string2

  7. Test if string length = 0 -z string • Test if string length > 0 -n filename • Equality i1 -eq i2 • Inequality i1 -neq i2 • Greater than i1 -gt i2 • Greater than or equal to i1 -ge i2 • Less than i1 -lt i2 • Less than or equal to i1 -le i2 • Negate !

  8. Case case variable in value1) ...;; value2) ...;; *)...;; esac case $f in “english”) echo .hello.;; “thai”) echo .sawasdee.;; esac

  9. While Loop while condition do ... done i=0 while [ $i -lt 10]; do echo “Hello” i=`expr $i + 1` done

  10. For Loop for variable [ in range ] do ... done for f in *.txt; do cat $f done

  11. echo command • \n new line • \a alert (bell) • \b backspace • \c suppress trailing new line • \r carriage return • \t horizontal tab

More Related