面试题3

关于shell脚本:
一、用Shell 编程,判断一文件是否是存在,若是存在将其拷贝到 /dev 目录下。shell

vi a.sh
#!/bin/bash
read -p "input your filename:" A
if [  ! -f $A ];then
    cp -f $A /dev
fi

二、shell脚本,判断一个文件是否存在,不存在就建立,存在就显示其路径编程

vi shell.sh
#!/bin/bash
read -p "请输入文件名:" file
if [ ! -f $file ];then
    echo "$file的路径:$(find / -name $file)"
else
    mkdir $file
    echo "$file 已存在"
fi

三、写出一个shell脚本,根据你输入的内容,显示不一样的结果bash

#!/bin/bash
read -p "请输入你的内容:" N
case $N in
    [0-9]*)
        echo "数字"
    ;;
    [a-z]|[A-Z])
        echo "小写字母"
    ;;  
    *)
        echo "标点符号、特殊符号等"
esac

四、写一个shell脚本,当输入foo,输出bar,输入bar,输出foo网络

vi shell.sh
#!/bin/bash
read -p "请输入【foo|bar】:" A
case $A in
    foo)
        echo "bar"
    ;;
    bar)
        echo "foo"
    ;;
    *)
        echo "请输入【foo|bar】其中一个"
esac

五、写出一个shell脚本,能够测试主机是否存活的ide

#!/bin/bash
单台主机:
ping  -c3 -w1 192.168.80.100
if [ $? -eq 0 ];then
    echo "该主机up"
else
    echo "该主机down"
fi
多台主机:
P=192.168.80.
for ip in {1..255}
do
    ping -c3 -w1 $P$ip
    if [ $? -eq 0 ];then
        echo "该$P$ip主机up"
    else
        echo "该$P$ip主机down"
    fi
done

六、写一个shell脚本,输出/opt下全部文件函数

vi shell.sh
第一种:
#!/bin/bash
find /opt -type f 
第二种:
#!/bin/bash
for A in $(ls /opt)
do
    if [ ! -f $A ];then
        echo $A
    fi
done

七、编写shell程序,实现自动删除50个帐号的功能。帐号名为stud1至stud50。测试

vi shell.sh
#!/bin/bash
i=1
while [ $i -le 50 ]
do
userdel -r stud$i
let i++
done

八、用shell脚本建立一个组class、一组用户,用户名为stdX X从1-30,并归属class组code

vi shell.sh
第一种:
#!/bin/bash
groupadd class
for X in std{1..30}
do
    useradd -G class $X
done
第二种:
#!/bin/bash
X=1
while [ $X -le 30 ]
do
    useradd -G class std$X
    let X++
done

九、写一个脚本,实现判断192.168.80.0/24网络里,当前在线的IP有哪些,能ping通则认为在线ip

vi shell.sh
#!/bin/bash
for ip in 192.168.80.{1..254}
do
    ping -c3 -w0.1 $ip  &> /dev/null
    if [ $? -eq 0 ];then
        echo "$ip 存活"
    else
        echo  "$ip 不存活"
    fi
done

十、写一个shell脚本,能够获得任意两个数字相加的和input

vi shell.sh
#!/bin/bash
sum = $(($1 + $2))
echo "$1 + $2 = $sum"

shell.sh 1 2

十一、定义一个shell函数运行乘法运算

#!/bin/bash
sum(){
    SUM=$(($1*$2))
    echo "$1*$2=$SUM"
}
sum $1 $2

十二、写出一个shell脚本,判断两个整数的大小,若是第一个整数大于第二个整数那就输出第一个整数,不然输出第二个整数

#!/bin/bash
if [ $1 -gt $2 ];then
        echo "$1大"
    else
        echo "$2大"
fi

1三、shell脚本,九九乘法表

vi shell.sh
#!/bin/bash
a=1
while [ $a -le 9 ]
do
    b=1
    while [ $b -le $a ]
    do
        echo -n -e "$a * $b = $(($a * $b))\t"
        let b++
    done
    echo ""
    let a++
done
相关文章
相关标签/搜索