Linux 程序设计---shell[1]:变量、条件、控制结构

版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/u012319493/article/details/87549634

变量

将$变量表达式放在双引号中,变量汇编替换为它的值;
如果放在单引号中,就不会发生替换。
$ 前面加 \ 可取消其特殊含义。
字符串通常被放在双引号中,以防止变量被空白字符分开,同时又允许 $ 扩展。

#!/bin/sh

myvar="Hi there"

echo $myvar    
echo "$myvar"
echo '$myvar'
echo \$myvar

echo Enter come text
read myvar

echo '$myvar' now equals $myvar
exit 0

结果:

yjp@yjp-VirtualBox:~/shell$ /bin/sh ./variable
Hi there
Hi there
$myvar
$myvar
Enter come text
aaa
$myvar now equals aaa

$1, $2, … 脚本程序的参数
$* 列出所有参数。如图 IFS 被修改,$* 将命令行分割为参数的方式也会改变
$@ 是 $* 的变体,不使用 IFS 环境变量。

#!/bin/sh

salutation="Hello"
echo $salutation
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 greetion"
read salutation

echo $salutation
echo "The script is now complete"
exit 0

结果:

yjp@yjp-VirtualBox:~/shell$ /bin/sh ./try_var foo var baz
Hello
The second parameter was var
The first parameter was foo
The parameter list was foo var baz
The user's home directory is /home/yjp
Please enter a new greetion
aaa
aaa
The script is now complete

条件

test 与 [
test -f <filename> 检查文件是否存在,等价于命令 [ -f <filename> ]
[ 和被检查的条件之间要有空格。
test -d <filename> 检查文件是否为目录

#!/bin/sh

if [ -f /bin/bash ]
then
	echo "file /bin/bash exists"
fi

if [ -d /bin/bash ]
then
	echo "bin/bash is a directory"
else
	echo "bin/bash is not a directory"
fi

结果:

yjp@yjp-VirtualBox:~/shell$ /bin/sh test
file /bin/bash exists
bin/bash is not a directory

控制结构

语法:

if <condition>
then
	<statements>
else
	<statements>
fi
#!/bin/sh

echo "Is it mornint? Please answer yes or no"
read timeofday

if [ "$timeofday" = "yes" ]; then
	echo "Good morning"
elif [ "$timeofday" = "no" ]; then
	echo "Good afternoon"
else
	echo "Sory, $timeofday not recognized. Enter yes or no"
	exit 1
fi

exit 0

结果:

yjp@yjp-VirtualBox:~/shell$ /bin/sh test
Is it mornint? Please answer yes or no
yes
Good morning

语法:

for <variable> in <values>
do
	<statements>
done
#!/bin/sh

for foo in bar fud 43
do	
	echo $foo
done
exit 0

结果:

bar
fud
43

使用通配符扩展的 for 循环:

#!/bin/sh

for file in $(ls t*); do
	echo $file
done
exit 0

结果:

test
try_var

条件为真时反复执行:

while <condition> do
	statements
done

密码检查程序:

#!/bin/sh

echo "Enter password"
read trythis

while [ "$trythis" != "secret" ]; do
	echo "Sorry, try again"
	read trythis
done
exit 0

结果:

Enter password
aaa
Sorry, try again
secret

循环将反复执行直到条件为真:

until <condition>
do
	<statememts>
done

设置一个警报,当某个特定的用户登录时,警报启动。如果用户已经登录,循环不需要执行:

#!/bin/sh

until who | grep "$1" > /dev/null
do
	sleep 60
done

echo -e 'a'
echo "*** $1 has just logged in ***"

exit 0

case 语句:

case <variable> in
	pattern [ | <pattern>] ...) <statements>;;
	pattern [ | <pattern>] ...) <statements>;;
	...
esac

双分号标记前一个模式的结束和后一个模式的开启。

用户输入:

#!/bin/sh

echo "Is it morning? Please answer yes or no"
read timeofday

case "$timeofday" in
	yes)	echo "Good Morning";;
	no)	echo "Good Afternoon";;
	y)	echo "Good Morning";;
	n)	echo "Good Afternoon";;
	*)	echo "Sory, answer no recognized";;
esac

exit 0

合并匹配模式:

#!/bin/sh

echo "Is it morning? Please answer yes or no"
read timeofday

case "$timeofday" in
	yes | y | Yes | YES)	echo "Good Morning";;
	n* | N*)		echo "Good Afternoon";;
	*)			echo "Sory, answer no recognized";;
esac

exit 0

执行多条语句:

#!/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 "Sory, answer no recognized"
		echo "Please answer yes or no"
		exit 1
		;;
esac

exit 0

结果:

Is it morning? Please answer yes or no
y
Good Morning
Up bright and early this morning

AND 列表:

<statement1> && <statement2> && <statement3>
#!/bin/sh

touch file_one
rm -f file_two

if [ -f file_one ] && echo "hello" && [ -f file_two ] && echo " there"
then
	echo "in if"
else
	echo "in else"
fi

exit 0

结果:

hello
in else

OR 列表:

#!/bin/sh

rm -f file_one

if [ -f file_one ] || echo "hello" || echo " there"
then
	echo "in if"
else
	echo "in else"
fi

exit 0

结果:

hello
in if

源码:《Linux 程序设计》

猜你喜欢

转载自blog.csdn.net/u012319493/article/details/87549634