Question requirements
Write a greeting program that can output greetings to the user according to the current time of the system when it is executed. Assume that from midnight to noon is morning, noon to 6 pm is afternoon, and 6 pm to midnight is evening.
Refer to the answer
if [-a] and the meaning
#!/bin/bash
d=`date +%H`
if [$d -ge 0 -a $d -lt 7 ]
then
tag=1
elif [$d -ge 7 -a $d -lt 12 ]
then
tag=2
elif [$d -ge 12 -a $d -lt 18 ]
then
tag=3
else
tag= 4
fi
case $tag in
1)
echo "Good morning"
;;
2)
echo "Good morning"
;;
3)
echo "Good afternoon"
;;
4)
echo "Good evening"
;;
*)
echo "The script went wrong"
;;
esac
Question requirements
Write a shell script, A simple pop-up menu function is realized, and the user can select and execute the corresponding command from the keyboard according to the displayed menu item.
Refer to the answer
Prefer the second way
#!/bin/bash
PS3="Please input your choice(1-4): "
select i in w ls pwd quit
do
case $i in
w)
w
;;
ls)
ls
;;
pwd)
pwd
;;
quit)
exit
;;
*)
echo "Please input 1-3."
;;
esac
done
Reference answer 2
#!/bin/bash
echo -e "1) w 2) ls 3) pwd 4) quit"
while :
do
read -p "Please input your choice(1- 4): "c
case $c in
1)
w
;;
2)
ls
;;
3)
pwd
;;
4)
exit
;;
*)
echo "Please input 1-4."
;;
esac
done
Question requirements
Write a shell script and check whether the specified user is logged in to the system every 5 minutes during execution , The user name is input from the command line. If the specified user has logged in, the related information will be displayed.
Reference answer
#!/bin/bash
while :
do
if w|sed '1'd|awk'{print $1}'|grep -qw "$1"
then
echo "User $1 has logged into the system."
exit
fi
sleep 300
done< /pre>Question requirements
First popularize a little common sense, we can use ps aux to view the PID of the process, and each PID will be generated in /proc. If the pid viewed is not in the proc, the process has been modified by someone, which means that the system has probably been passed.
Please use the above knowledge to write a shell, and regularly check whether your system has been used by othersRefer to the answer
#!/bin/bash
pp=$ $
ps -elf |sed '1'd> /tmp/pid.txt
for pid in `awk -v ppn=$pp'$5!=ppn {print $4}' /tmp/pid .txt`
do
if! [-d /proc/$pid ]
then
echo "There is no directory whose pid is $pid in the system. It needs to be checked."< br /> fi
doneQuestion requirements
Find a way to merge every three lines of text into one line.
For example: 1.txt content1
2
3
4
5
6
7It should be after processing
< pre>1 2 3
4 5 6
7Reference answer
#Wrap directly when encountering a line that is not divisible by three
#!/bin/bash
n=1
cat $1 |while read line
do
n1=$[$n%3]
if [$ n1 -eq 0 ]
then
echo "$line"
else
echo -n "$line "
fi
n=$[$n +1]
done