bài tập shell_full

35
1 BÀI TẬP SHELL CƠ BẢN Bài 1 /try_var.sh #!/bin/sh salutation="Hello" echo $salutation echo "The program $0 is now running" echo "The second parameter was $2" echo "The first parameter was $1" echo "The parameter list was $*" echo "The user's home directory is $HOME" echo "Please enter a new greeting" read salutation echo $salutation echo "The script is now complete" exit 0 ======= Bài 2 :/variable.sh #!/bin/sh myvar="Hi there" echo $myvar echo "message : $myvar" echo 'message : $myvar' echo "message :\$myvar" echo Enter some text read myvar echo '$myvar' now equals $myvar exit 0 ========

Upload: chuoi-qua

Post on 21-Jul-2015

19 views

Category:

Documents


0 download

TRANSCRIPT

1

BI TP SHELL C BN Bi 1 /try_var.sh #!/bin/sh salutation="Hello" echo $salutation echo "The program $0 is now running" echo "The second parameter was $2" echo "The first parameter was $1" echo "The parameter list was $*" echo "The user's home directory is $HOME" echo "Please enter a new greeting" read salutation echo $salutation echo "The script is now complete" exit 0 ======= Bi 2 :/variable.sh #!/bin/sh myvar="Hi there" echo $myvar echo "message : $myvar" echo 'message : $myvar' echo "message :\$myvar" echo Enter some text read myvar echo '$myvar' now equals $myvar exit 0 ========

2

Bi 3/and.sh #!/bin/sh touch file_one rm -f file_two if [ -f file_one ] && echo "hello" && [ -f file_two ] && echo "there" then echo -e "in if" else echo -e "in else" fi exit 0 ========= Bi 4 : /for.sh #!/bin/sh rm -rf fred* echo > fred1 echo > fred2 mkdir fred3 echo > fred4 for file in fred* do if [ -d "$file" ]; then break; fi done echo first directory fred was $file

3

exit 0 ====== Bi 5 : case1.sh #!/bin/sh echo "Is it morning? Please answer yes no" read timeofday case "$timeofday" in "yes") echo "Good Morning";; "no" ) echo "Good Afternoon";; "y" ) echo "Good Morning";; "n" ) echo "Good Afternoon";; * ) echo "Sorry, answer not recognised";; esac exit 0 ======= Bi 6 : case2.sh #!/bin/sh echo "Is it morning? Please answer yes no" read timeofday case "$timeofday" in "yes" | "y" | "Yes" | "YES" ) echo Morning";; "n*" | "N*" ) echo Afternoon";;

or

or

"Good "Good

4

* ) "Sorry, answer not recognised";; esac exit 0 ========

echo

Bi 7 : case3.sh #!/bin/sh echo "Is it morning? Please answer yes or no" read timeofday case "$timeofday" in "yes" | "y" | "Yes" | "YES" ) echo "Good Morning" echo "Up bright and early this morning?" ;; "[nN]*" ) echo "Good Afternoon" ;; * ) echo "Sorry, answer not recognised" echo "Please answer yes or no" exit 1 ;; esac exit 0

5

======= Bi 8 /cat_here.sh #! /bin/sh cat > test.txt fred2 mkdir fred3 echo > fred4 for file in fred* do if [ -d "$file" ]; then continue fi echo file is $file done exit 0 ======= Bi 13/dot_coma.sh #!/bin/sh echo "Inside script" PATH=/mypath/bin: /usr/local echo $PATH echo "Script end" ======= Bi 14/elseif_contr.sh #!/bin/sh

8

echo -n "Is it morning? Please answer yes or no: " read timeofday if [ "$timeofday" = "yes" ]; then echo "Good morning" elif [ "$timeofday" = "no" ]; then echo "Good afternoon" else echo "Sorry, $timeofday not recognized. Enter yes or no" exit 1 fi exit 0 ======== Bi 15/evaluate.sh #!/bin/sh x=0 while [ "$x" -ne 10 ] ; do echo $x x=$(($x + 1)) done exit 0 ======== Bi 16/exec_demo.sh #! /bin/sh echo "Try to execute mc program"

9

exec mc echo "you can not see this message !" ======== Bi 17 /exec_demo.sh #! /bin/sh echo "Try to execute mc program" exec mc echo "you can not see this message !" ===== Bi 18/export1.sh #!/bin/sh foo="This is foo" export bar="This is bar" ======= Bi 19/export2.sh #!/bin/sh echo "Value: $foo" echo "Value: $bar" ./export2.sh export1.sh #!/bin/sh foo="Th is foo" export bar="This is bar"

10

./export1.sh value: Value : This is bar ======== Bi 20/findit.sh #! /bin/sh # findit.sh -- simple search script by Luke - 07Feb2005 # _findit() { find . -name "*$_expr*" } echo "Enter search pattern: \c" read _expr if [ "$_expr" = "" ]; then echo "Do you REALLY want to list ALL the files? [y/n] \c" read _list if [ "$_list" = y ]; then _findit else exit fi fi _findit ========

11

Bi 21 /for1.sh #!/bin/bash echo "Can you see the following:" for (( i=1; i