第三章 shell的輸入和輸出
1.echo echo [option] string
-e 解析轉移字符
-n 回車不換行,linux系統默認回車換行
轉移字符 \c \t \f \n
#!/bin/bash
#echo
echo -e "this echo's 3 newlne\n\n\n"
echo "OK"
echo
echo "this is echo's 3 ewline\n\n\n"
echo "this log file have all been done">mylogfile.txt
[test@szbirdora ~]$ sh echod.sh
this echo's 3 newlnenode
OKlinux
this is echo's 3 ewline\n\n\n
上面能夠看到有-e則能夠解析轉移字符,沒有不能解析。echo空輸出爲空
shell
2.read 能夠從鍵盤或文件的某一行文本中讀入信息,並將其賦給一個變量
read variable1 variable2
eg.
#!/bin/bash
#readname
echo -n "first name:"
read firstname
echo -n "last name:"
read lastname
echo "this name is $firstname $lastname"
3.cat 顯示文件的內容,建立內容,還能夠顯示控制字符
cat [options]filename1 filename2
-v 顯示控制字符(Windows文件)
cat命令不會分頁顯示,要分頁能夠採用more、lessexpress
4.管道|bash
5.tee 把輸出的一個副本輸送到標準輸出,另外一個副本拷貝到相應的文件中,通常與管道合用
tee [options] files
-a 在文件中追加
eg.
[test@szbirdora 1]$ echo |tee myfileless
[test@szbirdora 1]$ cat myfile
將myfile文件置空
socket
6.文件重定向
command>filename ---覆蓋輸出
command>>filename ---追加輸出
command>filename>&1 ---把標準輸出和標準錯誤重定向
command<<delimiter ---輸入直到delimiter分解符
command<filename ----輸入文件內容到命令
command<- --- 關閉標準輸入
>nullfile.txt ---建立字節爲0的文件
command1<filename>command3 ---按從左到右順序執行
eg.
說明:myfile爲空間
[test@szbirdora 1]$ df -lh>myfile
[test@szbirdora 1]$ cat myfile
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 3.3G 16G 18% /
none 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 79G 17G 59G 23% /u01
/dev/sda4 28G 3.9G 22G 15% /u02
[test@szbirdora 1]$ df -lh>myfile
[test@szbirdora 1]$ cat myfile
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 3.3G 16G 18% /
none 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 79G 17G 59G 23% /u01
/dev/sda4 28G 3.9G 22G 15% /u02
[test@szbirdora 1]$ df -lh>>myfile
[test@szbirdora 1]$ cat myfile
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 3.3G 16G 18% /
none 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 79G 17G 59G 23% /u01
/dev/sda4 28G 3.9G 22G 15% /u02
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 3.3G 16G 18% /
none 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 79G 17G 59G 23% /u01
/dev/sda4 28G 3.9G 22G 15% /u02
[test@szbirdora 1]$ cat >>myfile<<exit
> China
> Hubei
> Suizhou
> exit
[test@szbirdora 1]$ cat myfile
China
Hubei
Suizhouide
7.exec 能夠用來替代當前shell。現有任何環境變量都會清除函數
第四章 控制流結構
1.if語句
if 條件1
then
命令1
elif 條件2
then
命令2
else
命令3
fi
------------------
if 條件
then 命令
fi
eg:
#!/bin/bash
#if test
#this is a comment line
if [ "10" -lt "12" ];then
#yes 10 is less than 12
echo "yes,10 is less than 12"
else
echo "no"
fi
注意:if語句必須以fi終止
"10" 前一個空格,「12」後也有一個空格。這個條件都是經過test命令來指定。條件表達爲test expression或者[expression]
條件表達式中的比較函數
man test
NAME
test - check file types and compare valuesoop
SYNOPSIS
test EXPRESSION
[ EXPRESSION ]
[ OPTION
DESCRIPTION
Exit with the status determined by EXPRESSION.
--help display this help and exit
--version
output version information and exit
EXPRESSION is true or false and sets exit status. It is one of:
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
both EXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
either EXPRESSION1 or EXPRESSION2 is true
[-n] STRING
the length of STRING is nonzero
-z STRING
the length of STRING is zero
STRING1 = STRING2
the strings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1 is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1 is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1 is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1 is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1 is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1 is not equal to INTEGER2
FILE1 -ef FILE2
FILE1 and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 is newer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 is older than FILE2
-b FILE
FILE exists and is block special
-c FILE
FILE exists and is character special
-d FILE
FILE exists and is a directory
-e FILE
FILE exists
-f FILE
FILE exists and is a regular file
-g FILE
FILE exists and is set-group-ID
-h FILE
FILE exists and is a symbolic link (same as -L)
-G FILE
FILE exists and is owned by the effective group ID
-k FILE
FILE exists and has its sticky bit set
-L FILE
FILE exists and is a symbolic link (same as -h)
-O FILE
FILE exists and is owned by the effective user ID
-p FILE
FILE exists and is a named pipe
-r FILE
FILE exists and is readable
-s FILE
FILE exists and has a size greater than zero
-S FILE
FILE exists and is a socket
-t [FD]
file descriptor FD (stdout by default) is opened on a terminal
-u FILE
FILE exists and its set-user-ID bit is set
-w FILE
FILE exists and is writable
-x FILE
FILE exists and is executable
eg.
#!/bin/bash
#if test
#this is a comment line
echo "Enter your filename:"
read myfile
if [ -e $myfile ]
then
if [ -s $myfile ];then
echo "$myfile exist and size greater than zero"
else
echo "$myfile exist but size is zero"
fi
else
echo "file no exist"
fi
[test@szbirdora 1]$ sh iftest.sh
Enter your filename:
11
11 exist but size is zero
2.case語句
case語句爲多選擇語句。
case 值 in
模式1)
命令1
;;
模式2)
命令2
;;
esac
eg.
#!/bin/bash
#case select
echo -n "enter a number from 1 to 3:"
read ans
case $ans in
1)
echo "you select 1"
;;
2)
echo "you select 2"
;;
3)
echo "you select 3"
;;
*)
echo "`basename $0`:this is not between 1 and 3">&2
exit;
;;
esac
3.for 循環
for循環通常格式:
for 變量名 in 列表 (列表以空格做爲分割)
do
命令1
命令2
done
eg:
#!/bin/bash
#forlist1
for loop in 1 2 3 4 5
do
echo $loop
done
4.until循環
until 條件
do
命令1
命令2
...
done
條件測試發生在循環末尾,因此循環至少能夠執行一次。
5.
while循環
while 命令 (能夠是一個命令也能夠是多個,作條件測試)
do
命令1
命令2
...
done
注意:若是從文件中讀入變量<filename要放到done後
6.break和continue控制
break跳出,continue跳過