answer
每题对应相同序号的 md 文件,问题与答案均置于 md 文件中。
360 实习生笔试题
执行此脚本前,需要先进行公钥认证,先将本机的公钥复制到目标主机上
!/bin/bash
${!#}-->$ #--->$$cmd=${!#}
logfile=$(mktemp)i=1
success=0
failed=0
for ip in $@;do
if [$i -eq $ # ];then
break
fi
ssh $ip $cmd &> $logfile
if [ $? -eq 0 ];then
((success++))
echo -e "\n\033[32m $ip | success >> \033[0m \n"
cat $logfile
else
((failed++))
echo -e "\n\033[31m $ip | failed >> \033[0m\n "
cat $logfile
fi
((i++))
done
echo -e "\n\033[32m success: $success || failed: $failed \033[0m"
脚本放在这里没有缩进了
!/bin/bash
Tmpfile1=$(mktemp)
Tmpfile2=$(mktemp)
Tmpfile3=$(mktemp)
ifconfig | grep '^[^[:space:]]' | awk '{print $1}' > $Tmpfile1
ifconfig | grep -A 1 '^[^[:space:]]' | awk '/addr:/ {print $2}' | awk -F ':' '{print $2}' > $Tmpfile2paste $Tmpfile1 $Tmpfile2 > $Tmpfile3
cat $Tmpfile3 | while read line;do
key=echo $line|awk '{print $1}'
value=echo $line |awk '{print $2}'
echo $key $value
done
!/bin/bash
save_file="/tmp/
date +%F
-cpuinfo"cpuinfo=
top -bn 1 | grep "Cpu"
if [ ! -e $save_file ] ;then
cat << EOF >> $save_file
注:
us:用户态使用的cpu时间比
sy:系统态使用的cpu时间比
ni:用做nice加权的进程分配的用户态cpu时间比
id:空闲的cpu时间比
wa:cpu等待磁盘写入完成时间
hi:硬中断消耗时间
si:软中断消耗时间
st:虚拟机偷取时间
EOFecho -e "\033[32m \t\t\t\t\t\tdate:
date +%F
\033[0m\n" >> $save_file
echo -e "times:\t\tus:\t\tsy:\t\tni:\t\tid:\t\twa:\t\thi:\t\tsi:\t\tst:\t\n" >> $save_file
fius=$(echo $cpuinfo | awk '{print $2}')
sy=$(echo $cpuinfo | awk '{print $3}')
ni=$(echo $cpuinfo | awk '{print $4}')
id=$(echo $cpuinfo | awk '{print $5}')
wa=$(echo $cpuinfo | awk '{print $6}')
hi=$(echo $cpuinfo | awk '{print $7}')
si=$(echo $cpuinfo | awk '{print $8}')
st=$(echo $cpuinfo | awk '{print $9}')hour=$(date +%H)
echo -e "${hour}\t\t${us}\t\t${sy}\t\t${ni}\t\t${id}\t${wa}\t\t${hi}\t\t${si}\t\t${st}" >> $save_file
然后创建一个定时任务,每小时收集一次cpu的信息:
0 */1 * * * /bin/bash /root/shell/cpu_info.sh &> /dev/null
cat会先将文件读入内存,如果文件过大,会撑爆内存,还有,脚本执行时间太长
!/bin/bash
LANG=en_US
Usage() {
echo "Usage: $0 Logfile"
}if [ $# -eq 0 ] ;then
Usage
exit 0
else
Log=$1
ficat $Log | while read line;do
Year=$(echo $line | awk '{print $4}'| awk -F '/' '{print $3}' |awk -F ':' '{print $1}')
Month=$(echo $line | awk '{print $4}'| awk -F '/' '{print $2}')
Day=$(echo $line | awk '{print $4}'| awk -F '/' '{print $1}' | sed 's/[//')
echo$line >> /tmp/log/$ {Year}-${Month}-${Day}
done
文件大小:
程序执行的结果,8.4M的文件执行了5分多钟,心累:
程序先输出定时任务,然后选择需要操作的序号,选择序号后再选择对其的操作 [ start | stop ],start 与 stop 是设置是否将其注释掉来控制的
!/bin/bash
[ $UID -ne 0 ] && echo "please use root to excute!"
[ -e /etc/init.d/functions ] && . /etc/init.d/functions || exittask_file=/var/spool/cron/root
crontabfile=$(mktemp)
crontab -l > $crontabfiledelete blank line
sed -i '/^$/d' $crontabfile
line=$(cat $crontabfile | wc -l)
crontab_list(){
echo -e "\n\033[32m crontab as follows:\033[0m\n"
cat -n $crontabfile
}input the number of modify
input_num(){
while true;do
echo -e "\n\033[32mplease input a task number:\033[0m\n"
read num
if [ "$num" -lt 1 -o "$num" -gt "$line" ];then
echo "input error!Please input again:"
continue
else
break
fi
done
}crontab_list
input_numoperate task
while true;do
read -p "please input to perform operations:[ stop | start | quit ] " do
case $do in
quit|q):
exit
;;
start):
task=$(head -n $num $crontabfile | tail -n 1)
echo "$task" | grep '^#' &> /dev/nullif [ $? -eq 0 ];then sed -i "${num}s/^#//" $crontabfile cat "$crontabfile" > $task_file action "success!" /bin/true break else echo "This task is already start.Pleasse input again:" fi ;; stop): task=$(head -n $num $crontabfile | tail -n 1) echo "$task" | grep '^[^#]' &> /dev/null if [ $? -eq 0 ];then sed -i "${num}s/^/#/" $crontabfile cat "$crontabfile" > $task_file action "success!" /bin/true break else echo "This task is already stop.Pleasse input again:" fi ;; *): echo "Input is error! Please input again:" ;;
esac
done
crontab_list
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.