面試題3

關於shell腳本:
1、用Shell 編程,判斷一文件是不是存在,如果存在將其拷貝到 /dev 目錄下。

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

2、shell腳本,判斷一個文件是否存在,不存在就創建,存在就顯示其路徑

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

3、寫出一個shell腳本,根據你輸入的內容,顯示不同的結果

#!/bin/bash
read -p "請輸入你的內容:" N
case $N in
    [0-9]*)
        echo "數字"
    ;;
    [a-z]|[A-Z])
        echo "小寫字母"
    ;;  
    *)
        echo "標點符號、特殊符號等"
esac

4、寫一個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

5、寫出一個shell腳本,可以測試主機是否存活的

#!/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

6、寫一個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

7、編寫shell程序,實現自動刪除50個賬號的功能。賬號名爲stud1至stud50。

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

8、用shell腳本創建一個組class、一組用戶,用戶名爲stdX X從1-30,並歸屬class組

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

9、寫一個腳本,實現判斷192.168.80.0/24網絡裏,當前在線的IP有哪些,能ping通則認爲在線

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

10、寫一個shell腳本,可以得到任意兩個數字相加的和

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

shell.sh 1 2

11、定義一個shell函數運行乘法運算

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

12、寫出一個shell腳本,判斷兩個整數的大小,如果第一個整數大於第二個整數那就輸出第一個整數,否則輸出第二個整數

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

13、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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章