bash, 1987, ang. Bourne
Transkrypt
bash, 1987, ang. Bourne
Bash ✦ Steve Bourne (sh, 1977), Brian Fox (bash, 1987, ang. Bourne-Again Shell), Chet Ramey (1990) ✦ http://steve-parker.org/sh/sh.shtml ✦ http://www.ooblick.com/text/sh/ ✦ env | grep SHELL ✦ $ bash ✦ $ echo Hello World Hello World $ ✦ #!/bin/sh # This is a comment! echo Hello World # This is a comment, too! ✦ $ chmod 755 first.sh $ ./first.sh Hello World $ Bash (c.d.) ✦ Zmienne ✦ $ env ✦ #!/bin/sh MY_MESSAGE="Hello World" echo $MY_MESSAGE ✦ $ chmod 755 var.sh $ ./var.sh Hello World $ ✦ $ MY_MESSAGE="Hello World" $ echo $MY_MESSAGE $ env $ export MY_MESSAGE $ env Bash (c.d.) ✦ Zmienne (c.d.) ✦ #!/bin/sh echo "What is your name?" read USER_NAME echo "Hello $USER_NAME" echo "I will create you a file called ${USER_NAME}_file" touch "${USER_NAME}_file" ✦ $ ./user.sh What is your name? zssz Hello zssz I will create you a file called zssz_file ✦ $ ls –l zssz_file -rw-r--r-- 1 zssz staff 0 paź 4 07:56 zssz_file Bash (c.d.) ✦ Pętle ✦ #!/bin/sh for i in 1 2 3 4 5 do echo "Looping ... number $i" done ✦ $ ./for.sh Looping ... number 1 Looping ... number 2 Looping ... number 3 Looping ... number 4 Looping ... number 5 Bash (c.d.) ✦ Pętle (c.d.) ✦ #!/bin/sh INPUT_STRING=hello while [ "$INPUT_STRING" != "bye" ] do echo "Please type something in (bye to quit)" read INPUT_STRING echo "You typed: $INPUT_STRING" done ✦ $ ./while.sh Please type something in (bye to quit) zssz You typed: zssz Please type something in (bye to quit) bye You typed: bye $ Bash (c.d.) ✦ Case ✦ $ ./talk.sh Please talk to me ... hello Hello yourself! zssz Sorry, I don't understand bye See you again! That's all folks! #!/bin/sh echo "Please talk to me ..." while : do read INPUT_STRING case $INPUT_STRING in hello) echo "Hello yourself!" ;; bye) echo "See you again!" break ;; *) echo "Sorry, I don't understand" ;; esac done echo "That's all folks!" Bash (c.d.) Command & && || ^ $ = ! $$ $! $? $0 $1 $9 $@ $* Description background Logical AND Logical OR Start of line End of line String equality (cf. -eq) Logical NOT PID of current shell PID of last background exit status Name of command Name of first parameter ninth parameter All parameters not preserving quoting Example ls & if [ "$foo" -ge "0" ] && [ "$foo" -le "9"] if [ "$foo" -lt "0" ] || [ "$foo" -gt "9" ] (!bash) grep "^foo" grep "foo$ " if [ "$foo" = "bar" ] if [ "$foo" != "bar" ] echo "my PID = $$" ls & echo "PID of ls = $!" ls ; echo "ls returned code $?" echo "I am $0" echo "My first argument is $1" echo "My ninth argument is $9" echo "My arguments are $@" echo "My arguments are $*" Bash (c.d.) Command -eq -ne -lt -le -gt -ge -z -n -nt -d -f -r -w -x parenthesis: ( ... ) Description Example Numeric Equality if [ "$foo" -eq "9" ] Numeric Inquality if [ "$foo" -ne "9" ] Less Than if [ "$foo" -lt "9" ] Less Than or Equal if [ "$foo" -le "9" ] Greater Than if [ "$foo" -gt "9" ] Greater Than or Equal if [ "$foo" -ge "9" ] String is zero length if [ -z "$foo" ] String is not zero length if [ -n "$foo" ] Newer Than if [ "$file1" -nt "$file2" ] Is a Directory if [ -d /bin ] Is a File if [ -f /bin/ls ] Is a readable file if [ -r /bin/ls ] Is a writable file if [ -w /bin/ls ] Is an executable file if [ -x /bin/ls ] Function definition function myfunc() { echo hello } Bash (c.d.) ✦ Potoki ✦ ps -ef | grep –ef named ✦ Przekierowanie ✦ stdin (0); stdout (1); stderr (2) ✦ stdout: ls – l >plik1; ls –l >>plik1 ✦ stdin: cat <plik1 ✦ <&digit – użyj digit jako stdin ✦ >&digit – użyj digit jako stdout ✦ ps 2>&1 >myps Bash (c.d.) ✦ Test (/bin/[) ✦ operacje logiczne ✦ ! – negacja, -a - koniunkcja, -o - alternatywa ✦ opcje ✦ -r - można czytać; -w - można pisać; -x – można wykonywać; -d – plik jest katalogiem, –f - plik jest zwykłym plikiem ✦ $ touch abc $ chmod 644 abc $ ./file.sh Podaj nazwe pliku! $ ./file.sh abc Mozna pisac i czytac abc $ chmod 444 abc $./file.sh abc Nie mozna pisac lub czytac abc #!/bin/sh if test $1 then if test -w $1 -a -r $1 then echo "Mozna pisac i czytac $1" else echo "Nie mozna pisac lub czytac $1" fi else echo "Podaj nazwe pliku!" fi Bash (c.d.) ✦ Debugging ✦ opcja –n tylko czytanie skryptu ✦ opcja –x wypisanie polecenia na stderr i wykonanie #!/bin/sh ps –ef > /dev/null set -x ps –ef > dev/null set +x ps –ef > /dev/null ✦ $ ./debug.sh + ps –ef ./debug.sh: line 4: dev/null: Nie ma takiego pliku ani katalogu + set +x