1.設計一個名為showme的script,藉由環境變數及一些Linux指令,使其可以顯示資訊如下:
$ showme
我是 <- login name
我的家在 <- home directory
我的家有下列這些東西:
<- ls home directory
我的殼是 <- shell name
這台電腦是 <- host name
我目前的工作目錄是 <- present woring directory
目前的工作目錄有下列這些東西:"
<- ls present woring directory
----
2.設計一個名為verbvar的script,利用read輸入一個簡單的英文動詞,在其前加入一個to印出原形,字尾加ing印出作為進行式,字尾加ed印出作為過去式。
$ verbvar
請輸一個動詞: work
原 形: to work
進行式: working
過去式: worked
----
3.設計一個名為addtwo的script,使其可以接收兩個數字參數,並將其相加後的結果顯示出來,如果參數個數不夠或太多,就不執行運算,而顯示一段訊息,提示出正確的執行方式必須給兩個參數。
$ ./addtwo 12 6
sum = 18
$
$ ./addtwo 12
Usage: ./addtwo number1 number2
$
----
4.設計一個名為mycal的script,使其可以接收三個數字參數,第一、三個參數為數字,第二個參數為運算子(+-*/其中之一)以多層的if計算其結果,並將其結果顯示出來,如果參數個數不夠或太多,就不執行運算,而顯示一段訊息,提示出正確的執行方式。如果中間的運算子是/,而第三個參數是0,則顯示除數不可為0
$ ./mycal 12 + 3
result = 15
$
$ ./mycal 12 - 3
result = 9
$
$ ./mycal 12 \* 3 # 注意:*為特殊符號,需加一倒斜線輸入
result = 36
$
$ ./mycal 12 / 3
result = 4
$
$ ./mycal 12 / 0
無法計算:除數不可為0
$
----
1.
#!/bin/sh
echo "I am $LOGNAME"
echo "My home in the $HOME"
echo "My home have `ls -l $HOME`"
echo "My shell is $SHELL"
echo "Host Name is $HOSTNAME"
echo "Working Directory is `pwd`"
echo "Directory have `ls -l`"
2.
#!/bin/sh
echo -n "Please enter word : "
read word
echo "one --> $word"
echo "two --> ${word}ed"
echo "three-> ${word}ing"
3.
#!/bin/sh
if [ "$#" = "2" ]
then
echo "$1 + $2 = $(($1+$2))"
else
echo "Please enter Usage: ./addtwo number1 number2"
fi
exit 0
4.
#!/bin/sh
#if [ "$#" = "3" ] && [ "$2" = "+" ]
[ "$2" = "-"]
[ "$2" = "*" ]
[ "$2" = "/" ]
if [ "$#" = "3" ]
then
case "$2" in
"+")
echo "$1 + $3 = $(($1+$3))"
;;
"-")
echo "$1 - $3 = $(($1-$3))"
;;
"*")
echo "$1 * $3 = $(($1*$3))"
;;
"/")
if [ "$3" = "0" ]
then
echo "number2 can't be zero"
else
echo "$1 / $3 = $(($1/$3))"
fi
;;
esac
else
echo "Please enter Usage: ./mycal number1 +,-,\*,/ number2"
fi
exit 0
2009年12月24日 星期四
2009年12月22日 星期二
Shell Script Programming II
5.設計一個名為getmax的script,使其可以接收兩個數字參數,將較大的數顯示出來,如果都一樣大,則顯示都一樣大。
$ ./maxmin 18 15
max = 18
$
$ ./maxmin 15 15
Equals
$
----
6.設計一個名為checkfile的script,使其可以接收一個檔案名稱作為參數,
若此名稱不是目前工作目錄的檔案或目錄,則顯示此名稱不存在,如果是檔案,則顯示其檔案內容,如果是一目錄則顯示此目錄內的清單。
$ checkfile 名稱
----
7.設計一個名為newerfile的script,使其可以接收兩個檔案名稱作為參數,並將日期較新的檔案名稱顯示出來。
$ newerfile file1.txt file2.txt
file2.txt 較新
----
8.設計一個名為avglen的script,使其可以接收一個檔案名稱(文字檔)作為參數,計算此檔平均每一列有多少bytes。
提示:使用wc -l < 檔名; 用man查一下wc的用法
$ avglen yang.txt
平均每列: 348 bytes
----
9.設計一個名為createtodayfile的script,以touch指令產生一個檔案,檔名中含有今天的日期,例如myfile20090706.txt。
date +%Y...., 其餘date的日期格式, 請以man指令查詢
----
10.設計一個名為addfiles的script,可自命令列讀取參數產生檔案allfile,如下,每加入一個檔案,隔2秒,如果重覆執行,則覆蓋原檔案。
原始檔案:
$ cat filea.txt
aaaaa
$ cat fileb.txt
bbbbb
$ cat filec.txt
ccccc
執行程式:
$ ./addfiles allfile.txt filea.txt fileb.txt filec.txt
結果:
$ cat allfile.txt
--2009-09-08 16:25:33
aaaaa
--2009-09-08 16:25:35
bbbbb
--2009-09-08 16:25:37
ccccc
5.
#!/bin/sh
if [ $1 -gt $2 ]
then
echo "$1 VS $2 -> Max is $1"
elif [ $1 -lt $2 ]
then
echo "$1 VS $2 -> Max is $2"
else
echo "$1 VS $2 -> Equals"
fi
exit 0
6.
#!/bin/sh
file=$1
if [ -f "$file" ]
then
echo "`cat $file`"
elif [ -d "$file" ]
then
echo "`ls -l $file`"
else
echo "No such file or directory"
fi
exit 0
7.
#!/bin/sh
if [ $1 -nt $2 ]
then
echo "$1 VS $2 = New file is $1"
elif [ $1 -ot $2 ]
then
echo "$1 VS $2 = New file is $2"
else
echo "No such file. Please check!"
fi
exit 0
8.
#!/bin/sh
x=`wc -c < $1`
y=`wc -l < $1`
echo total is "$x"bytes
echo line is "$y"
echo "average is $((x/y))bytes."
9.
#!/bin/sh
x=`date +%Y%m%d`
echo "creat a file ---> myfile$x".txt
`touch myfile"$x".txt`
10.
#!/bin/sh
echo 1.touch "$1"
touch $1
y=`date +%Y`
m=`date +%m`
d=`date +%d`
t=`date +%T`
echo --$y-$m-$d\ $t > $1
cat $2 >> $1
sleep 2
echo "run1"
t=`date +%T`
echo --$y-$m-$d\ $t >> $1
cat $3 >> $1
sleep 2
echo "run2"
t=`date +%T`
echo --$y-$m-$d\ $t >> $1
cat $4 >> $1
exit 0
$ ./maxmin 18 15
max = 18
$
$ ./maxmin 15 15
Equals
$
----
6.設計一個名為checkfile的script,使其可以接收一個檔案名稱作為參數,
若此名稱不是目前工作目錄的檔案或目錄,則顯示此名稱不存在,如果是檔案,則顯示其檔案內容,如果是一目錄則顯示此目錄內的清單。
$ checkfile 名稱
----
7.設計一個名為newerfile的script,使其可以接收兩個檔案名稱作為參數,並將日期較新的檔案名稱顯示出來。
$ newerfile file1.txt file2.txt
file2.txt 較新
----
8.設計一個名為avglen的script,使其可以接收一個檔案名稱(文字檔)作為參數,計算此檔平均每一列有多少bytes。
提示:使用wc -l < 檔名; 用man查一下wc的用法
$ avglen yang.txt
平均每列: 348 bytes
----
9.設計一個名為createtodayfile的script,以touch指令產生一個檔案,檔名中含有今天的日期,例如myfile20090706.txt。
date +%Y...., 其餘date的日期格式, 請以man指令查詢
----
10.設計一個名為addfiles的script,可自命令列讀取參數產生檔案allfile,如下,每加入一個檔案,隔2秒,如果重覆執行,則覆蓋原檔案。
原始檔案:
$ cat filea.txt
aaaaa
$ cat fileb.txt
bbbbb
$ cat filec.txt
ccccc
執行程式:
$ ./addfiles allfile.txt filea.txt fileb.txt filec.txt
結果:
$ cat allfile.txt
--2009-09-08 16:25:33
aaaaa
--2009-09-08 16:25:35
bbbbb
--2009-09-08 16:25:37
ccccc
5.
#!/bin/sh
if [ $1 -gt $2 ]
then
echo "$1 VS $2 -> Max is $1"
elif [ $1 -lt $2 ]
then
echo "$1 VS $2 -> Max is $2"
else
echo "$1 VS $2 -> Equals"
fi
exit 0
6.
#!/bin/sh
file=$1
if [ -f "$file" ]
then
echo "`cat $file`"
elif [ -d "$file" ]
then
echo "`ls -l $file`"
else
echo "No such file or directory"
fi
exit 0
7.
#!/bin/sh
if [ $1 -nt $2 ]
then
echo "$1 VS $2 = New file is $1"
elif [ $1 -ot $2 ]
then
echo "$1 VS $2 = New file is $2"
else
echo "No such file. Please check!"
fi
exit 0
8.
#!/bin/sh
x=`wc -c < $1`
y=`wc -l < $1`
echo total is "$x"bytes
echo line is "$y"
echo "average is $((x/y))bytes."
9.
#!/bin/sh
x=`date +%Y%m%d`
echo "creat a file ---> myfile$x".txt
`touch myfile"$x".txt`
10.
#!/bin/sh
echo 1.touch "$1"
touch $1
y=`date +%Y`
m=`date +%m`
d=`date +%d`
t=`date +%T`
echo --$y-$m-$d\ $t > $1
cat $2 >> $1
sleep 2
echo "run1"
t=`date +%T`
echo --$y-$m-$d\ $t >> $1
cat $3 >> $1
sleep 2
echo "run2"
t=`date +%T`
echo --$y-$m-$d\ $t >> $1
cat $4 >> $1
exit 0
2009年12月21日 星期一
Shell Script Programming III
2.設計一個名為manymaxmin的script,使其可以接收不固定的多個數字參數,顯示數字中最大的及最小的,如果都一樣大,則顯示都一樣大。
$ ./maxmin 12 18 9 32 24 13
max = 32
min = 9
$ ./maxmin 15 15 15 15 15 15
All Equal = 15
-----------------------------
#!/bin/sh
max=0
for x in $@
do
if [ $x -gt $max ]
then
max=$x
fi
done
min=9999999
for y in $@
do
if [ $y -lt $min ]
then
min=$y
fi
done
if [ $max -eq $min ]
then
echo All Equal = $max
else
echo Max = $max
echo Min = $min
fi
exit 0
--------------------------------
3.設計一個名為createmyfiles的script,以迴圈配合touch指令,產生十個檔案,檔名分別為myprog1.c,myprog2.c,...,myprog10.c。
#!/bin/sh
rm myprog*
for ((x=1;x<=10;x++))
do
touch myprog$x.c
done
exit 0
------------------------------
4.設計一個名為lastfiles的script,顯示目前目錄中最近被修改的n個檔案檔名。
$ ./lastfiles 5
a1.txt
a3.txt
...共5個
#!/bin/sh
ls -t | head -$1
---------------------------------
5.設計一個名為mvfiles的script,可以將所有副檔名為.c檔案,副檔名更名為.cpp,更名前後的副檔名均自命令列輸入。並將更名的結果顯示出來。
$ ./mvfiles .c .cpp
renamed myprog1.c to myprog1.cpp
....
#!/bin/sh
for file in *$1
do
mv $file `basename $file $1`$2
echo $file
done
exit 0
$ ./maxmin 12 18 9 32 24 13
max = 32
min = 9
$ ./maxmin 15 15 15 15 15 15
All Equal = 15
-----------------------------
#!/bin/sh
max=0
for x in $@
do
if [ $x -gt $max ]
then
max=$x
fi
done
min=9999999
for y in $@
do
if [ $y -lt $min ]
then
min=$y
fi
done
if [ $max -eq $min ]
then
echo All Equal = $max
else
echo Max = $max
echo Min = $min
fi
exit 0
--------------------------------
3.設計一個名為createmyfiles的script,以迴圈配合touch指令,產生十個檔案,檔名分別為myprog1.c,myprog2.c,...,myprog10.c。
#!/bin/sh
rm myprog*
for ((x=1;x<=10;x++))
do
touch myprog$x.c
done
exit 0
------------------------------
4.設計一個名為lastfiles的script,顯示目前目錄中最近被修改的n個檔案檔名。
$ ./lastfiles 5
a1.txt
a3.txt
...共5個
#!/bin/sh
ls -t | head -$1
---------------------------------
5.設計一個名為mvfiles的script,可以將所有副檔名為.c檔案,副檔名更名為.cpp,更名前後的副檔名均自命令列輸入。並將更名的結果顯示出來。
$ ./mvfiles .c .cpp
renamed myprog1.c to myprog1.cpp
....
#!/bin/sh
for file in *$1
do
mv $file `basename $file $1`$2
echo $file
done
exit 0
訂閱:
文章 (Atom)
標籤
- 上課實作 (7)
- 高級實作 (3)
- 雜貨店 (6)
- Exchange Server (3)
- grub組態設定 (1)
- IIS架站 (1)
- Juniper_SSG (4)
- L2 switch (2)
- L3 switch (4)
- Server2003 (6)
- Server2008 (5)
- Shell Script Programming (3)
- TCP/IP (1)
- Wirless_Aruba (4)