码迷,mamicode.com
首页 > 系统相关 > 详细

linux-practice(25-30)

时间:2017-11-22 13:15:53      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:算术运算符脚本

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)

标签:算术运算符脚本

原文地址:http://12496428.blog.51cto.com/12486428/1984026

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!