bash 面试题

1、设计一个shell脚本,在/userdata目录下建立10个目录,即user1~user50,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行

[root@node1 script]# cat mkuserdata.sh
#!/bin/bash
# author jin.ren
# date 20170412
# version 3.0
# can mkdir /usrdata userfile
if [ -d /usrdata ]; then
   for i in {1..10};do
mkdir /userdata/user$i
echo "mkdir user$i"
       chmod 754 /userdata/user$i
   done
else
   for b in {1..10};do
mkdir -p /userdata/user$b
       chmod 754 /userdata/user$b
echo "mkdir user$b"
   done
fi

2、用shell编程,判断一文件是不是块设备文件,如果是则将其拷贝到/dev目录下

[root@node1 script]# cat checkblock.sh
#!/bin/bash
# Author jin.ren
# version 3.0
# date 20170412
directory=/dev
read -p "input the files absolute path " file1
for file in $file1
   do
   if [ -b $file ]
then
cp  -a $file $directory/
echo  "cp block file $file is backup in $directory"
   fi
done
[root@node1 script]# checkblock.sh
input the files absolute path /root/sr0 /root/sda5 /root/bbb
cp block file /root/sr0 is backup in /dev
cp block file /root/sda5 is backup in /dev

3、用shell脚本,批量建立用户user1-user20的用户和组class1并为用户统一创建密码为www.magedu.com,并指定属组为class1

[root@node1 script]# cat useradd.sh
#!/bin/bash
#author jin.ren
#version 3.0
#date 20170412
groupadd class1
for i in {1..20}
   do useradd user$i  && echo "www.magedu.com" | passwd --stdin root &> /dev/null
      echo "add user user$i finished ... "
      usermod -g class1 user$i
      echo "user user$i add group class1"
done

4、编写shell程序,实现自动删除20个账号。账号名为user1至user20,用while语法实现

[root@node1 script]# cat userdel.sh
#!/bin/bash
# Author jin.ren
# Version 3.0
# date 20170412
a=1
while [ $a -le 20 ]
   do
userdel -r user$a
       a=$[$a+1]
done

5、写一个shell,循环打印1-80的随机数10次

[root@node1 script]# cat random.sh
#!/bin/bash
# Author jin.ren
# Version 3.0
# Date 20170412

for i in {1..10}; do
echo $[RANDOM%81]
done

6、用find命令查找/data/backup 文件下面创建时间为7天前的,后缀为.html的文件删除

[root@node1 script]# touch -m -t "201612051232.22" /data/backup/adfadf.html
[root@node1 script]# ll /data/backup/adfadf.html
-rw-r--r-- 1 root root 0 Dec  5 12:32 /data/backup/adfadf.py
[root@node1 script]# find /data/backup/ -type f -mtime +7 -name "*html" | xargs rm
[root@node1 script]# ls /data/backup/adfadf.html
ls: cannot access /data/backup/adfadf.py: No such file or directory

7、新建一个html.sh脚本,可以建立以8位随机字母开头,且后缀名为.html的文件,可以与用户交互接收参数,参数为多少时,可以新建多个这样的随机数文件。要求随机字母必需为大字母。

[root@node1 test]# cat html.sh
#!/bin/bash
# Author jin.ren
# Version 3.0
# Date 20170421
read -p "Please input the create the html number: " num
[ -z $num ] && num=$num
for I in `seq $num`
   do random=` cat /dev/urandom | tr -dc [:alpha:] | head -c${1-8}`
      touch $random\.html
      echo "create $random\.html"
      unset random
done
[root@node1 test]# sh html.sh
Please input the create the html number: 5
create exCoThER\.html
create BMgCylNn\.html
create VKlpRWyy\.html
create JNvdHWjM\.html
create NFpfpinc\.html
[root@node1 test]# ls
BMgCylNn.html  exCoThER.html  html.sh  JNvdHWjM.html  NFpfpinc.html  VKlpRWyy.html

8、写一个脚本判断今天是星期几,并实现七天中每天完成不一样的任务,假如今天是周四,
输出今天是周四,而后执行一条 tty命令

[root@node1 script]# cat weekday.sh
#!/bin/bash
# Author jin.ren
# Version 3.0
# Date 20170412
date1="Monday"
date2="Tuesday"
date3="Wednesday"
date4="Thursday"
date5="Friday"
date6="Saturday"
date7="Sunday"
for week in `date +%A`;do
if [ $week == $date1 ];then
  echo "This is $week" && ls -l
elif [ $week == $date2 ];then
  echo "This is $week" && pwd
elif [ $week == $date3 ];then
  echo "This is $week" && cal
elif [ $week == $date4 ];then
  echo "This is $week" && tty
elif [ $week == $date5 ]; then
  echo "This is $week" && id
elif [ $week == $date6 ]; then
  echo "This is $week" && du -sh
else  
  echo "This is $week " &&  history -r
fi
done
[root@node1 script]# weekday.sh
This is Wednesday
    April 2017    
Su Mo Tu We Th Fr Sa
                  1
2  3  4  5  6  7  8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30

9、生成指定个随机数,并计算出指定个随机数中的最大数,与最小数

[root@node1 test]# cat opertion.sh
#!/bin/bash
declare -i MAX=0
declare -i MIN=0
for i in `seq $1`;do
MYRAND=$RANDOM
[ $i  -eq 1 ] && MIN=$MYRAND
if [ $i -le $1 ] ; then
echo -ne "n$MYRAND  \t"
else
echo "$MYRAND"
fi
[ $MYRAND -gt $MAX ] && MAX=$MYRAND
[ $MYRAND -lt $MIN ] && MIN=$MYRAND
done
echo  "this number MAX=$MAX MIN=$MIN"
[root@node1 test]# sh opertion.sh 10
n30202  n400    n11091  n30524  n5947   n12782  n9286n6977     n25802   n5945   this number MAX=30524 MIN=400

10、用until命令来实现,统计文件行数的脚本,并且实现与用户交互,直到用户输入quit为止,退出程序,否则一直提示用户输入一个文件。

[root@node1 script]# cat wcfile.sh
#!/bin/bash
# Author: jin.ren
# Version: 3.0
# Date 20170412
read -p "please input a file way name or quit : " file
until [ $file == 'quit' ] ; do
    line=`cat $file | wc -l `
    echo "The file have a $line line! "

 read -p "please input a file way name: " file
 done

11、如何将/root/script添加至环境变量中去

[root@node1 ~]# mkdir script
[root@node1 ~]# cd script/
[root@node1 script]# cat <<EOF >> /script/test.sh
> #!/bin/bash
> echo "test PATH"
> EOF
[root@node1 script]# chmod +x test.sh
[root@node1 script]# test 这个时候用补全是补不出test.sh这个可执行脚本来的
test        testgdbm    testlibraw  testparm    testrb  
[root@node1 script]# cat <<EOF >> /etc/profile.d/script.sh
> export PATH=/root/script:$PATH
> EOF
[root@node1 script]# chmod +x /etc/profile.d/script.sh
[root@node1 script]# . /etc/profile.d/script.sh
[root@node1 ~]# test.sh
test PATH  当把/root/script添加至环境变量中时,test.sh 是可以直接执行了

12、设计一个shell脚本,在/data目录下创建10个目录,即user1~user10,并设置每个目录的权限,其中文件所有者的权限为可读可写可执行,文件属组的权限为可读可执行,其他用户的权限为可读。

#!/bin/bash
#
for i in {1..10};do
echo "$i"
mkdir -pv -m 754 "/data/user$i"
done

13、设计一个shell脚本,接收一个整数N作为参数,实现批量创建用户,用户名格式为user1到userN,一共N个用户。这些用户的基本组统一为class23。并删除之前创建的用户。

#!/bin/bash
#

var_cnum=$1
var_dnum=$1
var_group=$2

groupadd $var_group

while [ $var_cnum -gt 0 ];do
# echo $var_a
useradd user$var_cnum -g $var_group &> /dev/null
let var_cnum-=1
done
#delete users
cat /etc/passwd | grep "^user"

for i in $( seq 1 $var_dnum );do

userdel -rf user$i
done

14、设计一个shell脚本,该脚本可以接收一个1到5的整数作为参数,功能为创建文件,文件的名称格式为8个随机的大小英文字母,创建文件的个数等于输入的整数参数值。

#!/bin/bash

var_fnum=$1
var_content="A B C D E F G H I G K L M N O P Q R S T U V W X Y Z"
for i in $( seq 1 $var_fnum );do

var_n1=0
var_n2=0
var_n3=0
var_n4=0
var_n5=0
var_n6=0
var_n7=0
var_n8=0

for i in {1..8};do
var_index=$[ $RANDOM % 26 + 1 ]
var_alpha=`echo $var_content | cut -d " " -f $var_index`

if [ $i -eq 1 ];then
var_n1=$var_alpha
fi

if [ $i -eq 2 ];then
var_n2=$var_alpha
elif [ $i -eq 3 ];then
var_n3=$var_alpha
elif [ $i -eq 4 ];then
var_n4=$var_alpha
elif [ $i -eq 5 ];then
var_n5=$var_alpha
elif [ $i -eq 6 ];then
var_n6=$var_alpha
elif [ $i -eq 7 ];then
var_n7=$var_alpha
else
var_n8=$var_alpha
fi
done

echo "<h1>www.Magedu.com</h1>" > $var_n1$var_n2$var_n3$var_n4$var_n5$var_n6$var_n7$var_n8\.html

done

15、编写一个猜数字的游戏,要猜的数字为1到100百的整数。用户执行程序后根据提示输入一个数字,当输入的数字等于要猜的数字时,提示猜数字成功,程序结束退出。若输入的数字不等于要猜的数字,提示输入的数字大于或者小于要猜的数字,然后提示用户继续输入一个数字继续猜,直到用户猜中结果,程序猜结束退出。

#!/bin/bash
var_Matrix=$[ $RANDOM % 100 + 1 ]
echo $var_Matrix

var_num=0

while [ ! $var_Matrix -eq $var_num ];do
read -p "please input a number to guess the Matrix: " var_num
if [ $var_Matrix -gt $var_num ];then
echo "Your number is so smaller! try again!"
else
echo "Your number is so bigger! try again!"
fi
done

echo "you win the game, The Matrix is $var_Matrix"

原创文章,作者:renjin,如若转载,请注明出处:http://www.178linux.com/79317