1. 程式人生 > >shell面試題總結

shell面試題總結

用戶 date adduser /dev/null dir then light /dev/ lin

1.寫一個腳本,實現判斷10.0.0.0/24網絡裏,當前在線用戶的IP有哪些(方法有很多)
#!/bin/sh
for n in `seq 254`
do
  ping -c1 -w1 10.0.0.$n &>/dev/null
  if [ $? -eq 0 ]
  then
      echo "10.0.0.$n is up ">>/tmp/uplist.log
  else
      echo "10.0.0.$n is down ">>/tmp/downlist.log
  fi
done

2.批量創建10個系統帳號oldboy01-oldboy10並設置密碼(密碼為隨機8位字符串)。 

#! /bin/bash
. /etc/init.d/functions
  
Path=/server/scripts
UserDb=$Path/user.db
FailDb=$Path/fail_user.db
  
[ -d "$Path" ] || mkdir-p $Path
[ -f "$UserDb" ] ||touch $UserDb
[ -f "$FailDb" ] ||touch $FailDb
  
for n in $(seq -w 10)
 do
    passwd=`echo $(date+%t%N)$RANDOM|md5sum|cut -c 2-9`
    useradd oldboy$n >&/dev/null&& user_status=$?
    echo "$passwd"|passwd --stdinoldboy$n >&/dev/null && pass_status=$?
    if [ $user_status -eq 0 -a $pass_status -eq0 ];then
       action "adduser oldboy$n"/bin/true
       echo -e "user:\toldboy$npass:$passwd" >>$UserDb
    else
       action "adduser oldboy$n"/bin/false
       echo -e "user:\toldboy$npass:$passwd" >>$FailDb
    fi
done

  

shell面試題總結