셸 프로 그래 밍 선택 구조
bash 의 if 문 구 는 if 줄 에서 정 의 된 명령 을 실행 합 니 다.명령 의 종료 상태 코드 가 0 이면 then 뒤의 모든 명령 을 실행 합 니 다.명령 의 종료 상태 코드 가 0 값 이 아니라면 다음 명령 은 실행 되 지 않 습 니 다.
bash 에서 if 문 구 는 여러 가지 구조 가 있 습 니 다.
:
if ;then
1
2
......
fi
:
if ;then
1
2
......
else
1
2
......
fi
1:
if ;then
1
2
......
elif ;then
1
2
......
else
1
2
......
fi
2:
if ;then
1
2
......
if 2;then
1
2
......
else
1
2
......
fi
else
1
2
......
if 2;then
1
2
......
else
1
2
......
fi
fi
test 명령:
test if-then 。
test true,test 0 ,
false, test 0 。
test :
:
if test condition;then
1
2
.....
fi
:
if [ condition ];then // : conditon
1 // :if
2
.....
fi
일반적인 조건 판단 유형:
:
-eq
-gt
-lt
-ge
-le
-ne
:
#!/bin/bash
#
var1=10
var2=11
if [ $var1 -gt 5 ];then
echo "The test value $var1 is greater than 5."
fi
if [ $var1 -eq $var2 ];then
echo "The values are equal."
else
echo "The values are different."
fi
:
[root@localhost ~]# chmod +x 21.sh
[root@localhost ~]# ./21.sh
The test value 10 is greater than 5.
The values are different.
#!/bin/bash
#
var1=`echo "scale=4;10 / 3"| bc`
echo "The test value is $var1"
if [ $var1 -gt 3 ];then
echo "The result is larger than 3"
fi
:
[root@localhost ~]# chmod +x 24.sh
[root@localhost ~]# ./24.sh
The test value is 3.3333
./24.sh: line 5: [: 3.3333: integer expression expected
// ?test
//bash 。 bc , bash
。
:
=
!=
-n str1 str1 0 (true) (false)
-z str1 str1 0 (true) (false)
:
root
#!/bin/bash
#
testuser=root
if [ $USER = $testuser ]
then
echo "Welcome $testuser"
fi
:
[root@localhost ~]# chmod +x 25.sh
[root@localhost ~]# whoami
root
[root@localhost ~]# ./25.sh
Welcome root
#!/bin/bash
#
testuser=baduser
if [ $USER != $testuser ];then
echo "This isn't $testuser"
else
echo "Welcome $testuser"
fi
:
[root@localhost ~]# chmod +x 26.sh
[root@localhost ~]# whoami
root
[root@localhost ~]# ./26.sh
This isn't baduser
#!/bin/bash
#
var1=baseball
var2=hockey
if [ $var1 \> $var2 ];then
echo "$var1 is greater than $var2"
else
echo "$var1 is less than $var2"
fi
:
[root@localhost ~]# chmod +x 27.sh
[root@localhost ~]# ./27.sh
baseball is less than hockey
: ASCII
> $var2 ];then
echo "$var1 is greater than $var2"
else
echo "$var1 is less than $var2"
fi
:
[root@localhost ~]# chmod +x 28.sh
[root@localhost ~]# ./28.sh
Testing is less than testing
:
#!/bin/bash
#
var1=testing
var2=''
if [ -n $var1 ];then
echo "The string $var1 is not empty"
else
echo "The string $var1 is empty"
fi
if [ -z $var2 ];then
echo "The string $var2 is empty"
else
echo "The string $var2 is not empty"
fi
if [ -z $var3 ];then
echo "The string $var3 is empty"
else
echo "The string $var3 is not empty"
fi
:
[root@localhost ~]# chmod +x 30.sh
[root@localhost ~]# ./30.sh
The string testing is not empty
The string is empty
The string is empty
: shell
。 ,
, -n -z
。
:
-d file file
-e file file
-f file file
-r file file
-s file file
-w file file
-x file file
-O file file
-G file file
file1 -nt file2 file1 file2
file1 -ot file2 file1 file2
:
:
#!/bin/bash
#
if [ -d $HOME ];then
echo "Your HOME directory exists"
cd $HOME
else
echo "There's a problem with your HOME directory"
fi
:
[root@localhost ~]# chmod +x 31.sh
[root@localhost ~]# ./31.sh
Your HOME directory exists
, -e
#!/bin/bash
#
if [ -e $HOME ];then
echo "OK on the directory,now let's check the file"
if [ -e $HOME/testing ];then
echo "Appending date to existing file"
date >>$HOME/testing
else
echo "Creating new file"
date >>$HOME/testing
fi
else
echo "Sorry,you don't have a HOME directory"
fi
:
[root@localhost ~]# chmod +x 32.sh
[root@localhost ~]# ./32.sh
OK on the directory,now let's check the file
Creating new file
[root@localhost ~]# cat testing
Thu Jun 12 11:39:26 PDT 2014
-e
-f
-d
#!/bin/bash
#
if [ -e $HOME ];then
echo "The object exists,is it a file?"
if [ -f $HOME ];then
echo "Yes,it's a file"
else
echo "No,it's not a file"
if [ -f $HOME/.bash_history ];then
echo "But this is a file"
fi
fi
else
echo "Sorry,the object doesn't exists"
fi
:
[root@localhost ~]# chmod +x 33.sh
[root@localhost ~]# ./33.sh
The object exists,is it a file?
No,it's not a file
But this is a file
,
#!/bin/bash
#
pwfile=/etc/shadow
if [ -f $pwfile ];then
if [ -r $pwfile ];then
tail $pwfile
else
echo "Sorry,I'm unable to read the $pwfile file"
fi
else
echo "Sorry,the file $pwfile doesn't exist"
fi
:
[root@localhost ~]# chmod +x 34.sh
[root@localhost ~]# ./34.sh
ntp:!!:16203::::::
apache:!!:16203::::::
saslauth:!!:16203::::::
postfix:!!:16203::::::
pulse:!!:16203::::::
sshd:!!:16203::::::
tcpdump:!!:16203::::::
admin:$1$ThVHQoOH$mf66dh2AU.pZh1ky0olou1:16203:0:99999:7:::
student:!!:16229:0:99999:7:::
student1:!!:16229:0:99999:7:::
#!/bin/bash
#
file=t1test
touch $file
if [ -s $file ];then
echo "The $file exists and has data in it"
else
echo "The $file not exists or is empty"
date >>$file
fi
if [ -s $file ];then
echo "The $file file has data in it"
else
echo "The $file file is still empty"
fi
:
[root@localhost ~]# chmod +x 35.sh
[root@localhost ~]# ./35.sh
The t1test not exists or is empty
The t1test file has data in it
#!/bin/bash
#
logfile=$HOME/t1test
touch $logfile
chmod u-w $logfile
now=`date +%Y%m%d-%H%M`
if [ -w $logfile ];then
echo "The program run at: $now" >$logfile
echo "The first attemp successed"
else
echo "The first attemp failed"
fi
chmod u+w $logfile
if [ -w $logfile ];then
echo "The program run at: $now" >$logfile
echo "The second attemp successed"
else
echo "The second attemp failed"
fi
:
[root@localhost ~]# ./36.sh
The first attemp failed
The second attemp successed
#!/bin/bash
#
if [ -x 22.sh ];then
echo "You can run the script."
./22.sh
else
echo "Sorry,you are unable to execute the script."
fi
:
[root@localhost ~]# chmod +x 37.sh
[root@localhost ~]# ./37.sh
You can run the script.
#!/bin/bash
#
if [ -O /etc/passwd ];then
echo "You're the owner of the /etc/passwd file"
else
echo "You're not the owner of the /etc/passwd file"
fi
:
[root@localhost ~]# ./38.sh
You're the owner of the /etc/passwd file
#!/bin/bash
#
if [ -G $HOME/testing ];then
echo "Same group"
else
echo "Not same group"
fi
:
[root@localhost ~]# ./39.sh
Same group
#!/bin/bash
#
if [ ./22.sh -nt ./39.sh ];then
echo "22.sh new"
else
echo "39.sh new"
fi
if [ ./22.sh -ot ./39.sh ];then
echo "22.sh old"
else
echo "39.sh old"
fi
:
[root@localhost ~]# chmod +x 40.sh
[root@localhost ~]# ./40.sh
39.sh new
22.sh old
:-nt -ot
flase
복합 조건 검사:
[ condition1 ] && [ condition2 ]
then
[ condition1 ] || [ condition2 ]
then
:
#!/bin/bash
#
if [ -d $HOME ] && [ -w $HOME/testing ];then
echo "The file exists and you can write to it"
else
echo "I can't write to the file"
fi
:
[root@localhost ~]# chmod +x 41.sh
[root@localhost ~]# ./41.sh
The file exists and you can write to it
쌍 괄호 사용 하기:
쌍 괄호 안에 서 는 수학 할당 식 이나 수학 비교 표현 식 을 사용 할 수 있 습 니 다.
:
#!/bin/bash
#
var1=10
if (($var1 ** 2 > 90));then
((var2=$var1 ** 2))
echo "The square of $var1 is $var2"
fi
:
[root@localhost ~]# ./42.sh
The square of 10 is 100
:
이중 괄호 사용 하기:
패턴 매 칭 가능: 문자열 과 일치 하 는 정규 표현 식 을 정의 할 수 있 습 니 다.
:
#!/bin/bash
#
if [[ $USER == r* ]];then
echo "Hello $USER"
else
echo "Sorry,I don't know you"
fi
:
[root@localhost ~]# ./43.sh
Hello root
:
case 구문:
:
#!/bin/bash
#
case $USER in
rich | root)
echo "Welcome,$USER"
echo "Please enjoy your visit";;
testing)
echo "Special testing account";;
*)
echo "Sorry,you're not allowed here";;
esac
:
[root@localhost ~]# chmod +x 45.sh
[root@localhost ~]# ./45.sh
Welcome,root
Please enjoy your visit
연습 문제:
1, , 。
#!/bin/bash
#
UserName=user1
if id $UserName &> /dev/null;then
echo "$UserName exists."
fi
2, UID SHELL。
#!/bin/bash
#
UserName=user1
if id $UserName &> /dev/null;then
echo `grep "$UserName:" /etc/passwd | cut -d: -f1,3`
fi
3, /dev/sda1 , 。
4, /etc/rc.d/rc.sysinit , 。
5, , ID 499, ,
, 。
6, , , , :
7, , UID GID, “good guy” “bad guy”
8, , uid 0,
,
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Linux Shell 프로 그래 밍 - 텍스트 처리 grep, sed사용자 가 지정 한 '모드' 에 따라 대상 텍스트 를 일치 하 게 검사 하고 일치 하 는 줄 을 인쇄 합 니 다. ##포함 되 지 않 음, 역방향 일치 \ ##키워드 앞 뒤 가 맞지 않 고 키워드 만 일치 합 니 다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.