linux-practice(25-30)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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


[[email protected] ~]# vi w25.sh

[[email protected] ~]#  

[[email protected] ~]# chmod +x w25.sh

[[email protected] ~]# 

[[email protected] ~]# ./w25.sh little

little is exist.

[[email protected] ~]# 


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



[[email protected] ~]# vi w26.sh

[[email protected] ~]# 

[[email protected] ~]# chmod +x w26.sh 

[[email protected] ~]# 

[[email protected] ~]# 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                 ??????

[[email protected] ~]# 

[[email protected] ~]# ./w26.sh 2.txt 3.txt

Error, the parameter is not exist.

[[email protected] ~]# ./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



[[email protected] ~]# vi w27.sh

[[email protected] ~]#  

[[email protected] ~]# chmod +x w27.sh

[[email protected] ~]# 

[[email protected] ~]# ./w27.sh w26.sh

file w26.sh

[[email protected] ~]# ./w27.sh 3.txt

file 3.txt

[[email protected] ~]# vi w27.sh

[[email protected] ~]#  

[[email protected] ~]# ./w27.sh 3.txt

3.txt: ASCII text

[[email protected] ~]#


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



[[email protected] ~]# vi w28.sh

[[email protected] ~]# chmod +x w28.sh

[[email protected] ~]# 

[[email protected] ~]# ./w28.sh 

Usage: w28.sh integer1 integer2 integer3

[[email protected] ~]# 

[[email protected] ~]# ./w28.sh 12 q 23

please input 3 integers.

[[email protected] ~]# 

[[email protected] ~]# ./w28.sh 12 13 14

max is 14

min is 12

max is 14

min is 12 

[[email protected] ~]# vi w28.sh

[[email protected] ~]# 

[[email protected] ~]# ./w28.sh 12 13 14

max is 14

min is 12

[[email protected] ~]#


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


[[email protected] ~]# vi w29.sh

[[email protected] ~]# chmod +x w29.sh

[[email protected] ~]# 

[[email protected] ~]# ./w29.sh 

[[email protected] ~]# 

[[email protected] ~]# echo $?

0

[[email protected] ~]# ./w29.sh 2 - 1

1

[[email protected] ~]# ./w29.sh 2 / 1

2.00

[[email protected] ~]# ./w29.sh 10 / 5

2.00

[[email protected] ~]# vi w29.sh

[[email protected] ~]# 

[[email protected] ~]# ./w29.sh 

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

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

[[email protected] ~]# vi +3 w29.sh

[[email protected] ~]#

[[email protected] ~]# ./w29.sh 

please input 3 parameter

[[email protected] ~]# 


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"


[[email protected] ~]# vi w30.sh

[[email protected] ~]# chmod +x w30.sh 

[[email protected] ~]#  

[[email protected] ~]# ./w30.sh 

simple sum is 2500

double sum is 2550

[[email protected] ~]# 


以上是关于linux-practice(25-30)的主要内容,如果未能解决你的问题,请参考以下文章

linux-practice(1-19)

linux-practice(20-22)

text CSS Prac

ruby hashing_prac.rb

每日一练_PAT_B_PRAC_1004客似云来

无法将参数'2'的'char *(*)[6]'转换为'char ***'为'void prac(int *,char ***)'(代码片