1. 程式人生 > >linux-practice(25-30)

linux-practice(25-30)

算術運算符腳本

25、編寫腳本:傳遞一個字符串給腳本,腳本會將該字符串當作用戶名,如果該用戶不存在,則添加之並為其設置與用戶名相同的密碼

答:

#!/bin/bash

#

if id $1 &> /dev/null; then

echo "$1 is exist."

else

useradd $1

echo "$1" | passwd --stdin $1 &> /dev/null

echo "create $1 successfully"

fi


[root@little ~]# vi w25.sh

[root@little ~]#

[root@little ~]# chmod +x w25.sh

[root@little ~]#

[root@little ~]# ./w25.sh little

little is exist.

[root@little ~]#


26、編寫腳本:將兩個文本文件的路徑傳遞給腳本作為其參數,如果有文件不存在,則結束腳本執行並報告錯誤信息;如果文件都存在,則比較兩個文件中哪個文件的行數多,返回行數多的文件的文件名。

答:

#!/bin/bash

#

if ! id "$1 && $2" &> /dev/null; then

echo "Error, the parameter is not exist."

exit

fi


line1=$(cat $1 | wc -l)

line2=$(cat $2 | wc -l)

if [ $line1 -gt $line2 ]; then

echo "$1"

else

echo "$2"

fi



[root@little ~]# vi w26.sh

[root@little ~]#

[root@little ~]# chmod +x w26.sh

[root@little ~]#

[root@little ~]# ls

2.txt ace bcf cd6 t1file.txt ??????

24p1.sh acf bcg cdrom w25.sh ??????

24p2.sh acg bde dest w26.sh ??????

24p3.sh ade bdf install.log zhengshu ??????

3.txt adf bdg install.log.syslog ??????

789 adg boot.iso k 67 ?????????

8yu anaconda-ks.cfg c1 my ks.cfg ??????

a123 bce c78m m.z ??????

[root@little ~]#

[root@little ~]# ./w26.sh 2.txt 3.txt

Error, the parameter is not exist.

[root@little ~]# ./w26.sh 24p1.sh 24p2.sh

Error, the parameter is not exist.


27、編寫腳本:給腳本傳遞一個路徑作為參數,如果該文件存在,判斷其文件類型。

答:

#!/bin/bash

#

if [ -a $1 ]; then

type1=$(file $1)

echo "$type1"

else

echo "$1 is not exist."

fi



[root@little ~]# vi w27.sh

[root@little ~]#

[root@little ~]# chmod +x w27.sh

[root@little ~]#

[root@little ~]# ./w27.sh w26.sh

file w26.sh

[root@little ~]# ./w27.sh 3.txt

file 3.txt

[root@little ~]# vi w27.sh

[root@little ~]#

[root@little ~]# ./w27.sh 3.txt

3.txt: ASCII text

[root@little ~]#


28、編寫腳本:給腳本傳遞三個整數,要求:

1) 如果用戶傳遞過來的不是三個參數,報告正確用法;

2) 如果三個參數中有非純數字字符串,報告錯誤並提示用戶輸入數字;

3) 從三個整數中的選出最大數和最小數,並顯示出來;

答:

#!/bin/bash

#

if [ $# -ne 3 ]; then

echo "Usage: $(basename $0) integer1 integer2 integer3"

exit

fi


if echo "$1 $2 $3" | grep "\<[^[:digit:]]\+\>" &> /dev/null; then

echo "please input 3 integers."

exit

fi


if [ $1 -gt $2 ]; then

if [ $1 -gt $3 ]; then

echo "max is $1"

if [ $2 -gt $3 ]; then

echo "min is $3"

else

echo "min is $2"

fi

else

echo "max is $3"

echo "min is $2"

fi

exit

fi


if [ $2 -gt $1 ]; then

if [ $2 -gt $3 ]; then

echo "max is $2"

if [ $1 -gt $3 ]; then

echo "min is $3"

else

echo "min is $1"

fi

else

echo "max is $3"

echo "min is $1"

fi

exit

fi


if [ $3 -gt $2 ]; then

if [ $3 -gt $1 ]; then

echo "max is $3"

if [ $2 -gt $1 ]; then

echo "min is $1"

else

echo "min is $2"

fi

else

echo "max is $1"

echo "min is $2"

fi

exit

fi



[root@little ~]# vi w28.sh

[root@little ~]# chmod +x w28.sh

[root@little ~]#

[root@little ~]# ./w28.sh

Usage: w28.sh integer1 integer2 integer3

[root@little ~]#

[root@little ~]# ./w28.sh 12 q 23

please input 3 integers.

[root@little ~]#

[root@little ~]# ./w28.sh 12 13 14

max is 14

min is 12

max is 14

min is 12

[root@little ~]# vi w28.sh

[root@little ~]#

[root@little ~]# ./w28.sh 12 13 14

max is 14

min is 12

[root@little ~]#


29、傳遞三個參數給腳本,第一個為整數,第二個為算術運算符(包括基本的加、減、乘、除、取模運算符即可),第三個為整數,將計算結果顯示出來;要求:

1) 判斷第一個和第三個參數是否為整數;

2) 判斷第二個算術運算符必須是給定的運算符中的一個,否則報錯;

3) 如果是除法,在不能整除的情況下,保留兩位精度。

答:

#!/bin/bash

#

if [ $# -ne 3 ]; then

echo "please input 3 parameter"

exit

fi


if echo $1 | grep "^\<[[:digit:]]\+\>$" &> /dev/null; then

if echo $3 | grep "^\<[[:digit:]]\+\>$" &> /dev/null; then

if echo $2 | grep "+\|-\|*\|%" &> /dev/null; then

let result=$[$1$2$3]

echo "$result"

elif echo $2 | grep "\/" &> /dev/null; then

echo "scale=2;$1/$3" | bc

else

echo "$2 is error"

fi

fi

fi


[root@little ~]# vi w29.sh

[root@little ~]# chmod +x w29.sh

[root@little ~]#

[root@little ~]# ./w29.sh

[root@little ~]#

[root@little ~]# echo $?

0

[root@little ~]# ./w29.sh 2 - 1

1

[root@little ~]# ./w29.sh 2 / 1

2.00

[root@little ~]# ./w29.sh 10 / 5

2.00

[root@little ~]# vi w29.sh

[root@little ~]#

[root@little ~]# ./w29.sh

./w29.sh: line 3: syntax error near unexpected token `fi‘

./w29.sh: line 3: `fi [ $# -ne 3 ]; then‘

[root@little ~]# vi +3 w29.sh

[root@little ~]#

[root@little ~]# ./w29.sh

please input 3 parameter

[root@little ~]#


30、求100以內所有偶數之和以及奇數之和。

答:

#!/bin/bash

#

sum1=0

sum2=0

for i in $(seq 1 2 100); do

sum1=$[$sum1+$i]

done

echo "simple sum is $sum1"


for j in $(seq 2 2 100); do

sum2=$[$sum2+$j]

done

echo "double sum is $sum2"


[root@little ~]# vi w30.sh

[root@little ~]# chmod +x w30.sh

[root@little ~]#

[root@little ~]# ./w30.sh

simple sum is 2500

double sum is 2550

[root@little ~]#


linux-practice(25-30)