我在bash练习但是我在这个练习中遇到了很大的问题,你能帮助我吗?
这是练习:
创建一个shell脚本,该脚本将列出所有用户数量少于root且%mem的用户数至少大于根进程。对于这种类型的每个用户,脚本将创建一个文件.txt,其中列出了此类进程的PID,PPID,%MEM
可悲的是,我的尝试根本不起作用,它在第7行报告问题" ps:等待整数表达"。
#! /bin/bash
N_root=$(ps hax -o user | sort | uniq -c | grep root | awk '{print $1}') # here I save how many processes root has
mem_root=$(ps -u root -o %mem | sort | head -1) # I save the lowest %mem of root
for i in $(ps hax -o user | sort | uniq -c | sed '/root/d') # listing all processes
do
if [ $(echo $i | awk '{print $1}') -lt $N_root ] # i check if this user has less processes than root
then
user=$(echo $i | awk '{print $2}') # i save the name of the user
if [ $(ps -u $user -o %mem | sort -r | head -1) -gt $mem_root ] # check if the max %mem is greather than the lowest %mem of root
then
echo $user
touch <$user>.txt # creating file "<user>.txt"
for p in $(ps -u $user -o %mem,pid,ppid | sed '/PID/d' | sort -r) # listing all processis of current user ($n)
do
if [ $(echo $p | awk '{print $1}') -gt $mem_root ] # check if the process has more %mem than root
then
echo $p >> "<$user>.txt" # copying %mem,pid,ppid in the file
fi
done
fi
fi
done
答案 0 :(得分:0)
有很多方法可以解决这个问题。对于多重比较,我通常喜欢将值索引到数组中,然后通过值(如果可能)通过单个循环执行比较。适用于您的问题,它可能如下所示:
#!/bin/bash
## set output filename
ofn=./userpctmem.txt
## arrays to hold procs, users, and %mem
declare -a procarray
declare -a userarray
declare -a memarray
## read values into arrays
while read -r pval uval || [ -n "$uval" ]; do
procarray+=( "$pval" )
userarray+=( "$uval" )
memarray+=( $(ps -u "$uval" -o %mem | grep -v '%' | sort -r | head -n 1) )
done <<<"$(ps hax -o user | sort | uniq -c)"
## save root procs and %mem in separate variables for convenience
rproc=$(ps -u root | tail -n +2 | wc -l)
rmem=$(ps -u root -o %mem | grep -v '%' | sort -r | head -n 1)
## get the number of users found
nusers=${#userarray[@]}
## print the captured data (for verification)
printf "\n%-12s %-6s %s\n\n" "user" "nprocs" "maxmem"
for ((i=0; i<$nusers; i++)); do
printf " %-12s %5s %s \n" "${userarray[i]}" "${procarray[i]}" "${memarray[i]}"
done
## test user process less than root and %mem >= root (max %mem process)
# and write matching users to output
printf "\nUsers with nprocs less than root and %%mem at least as great as root\n\n"
for ((i=0; i<$nusers; i++)); do
[ "${userarray[i]}" == 'root' ] && continue
umem="${memarray[i]}"
if [ "${umem//.*/}" -ge "${rmem//.*/}" -a "${procarray[i]}" -lt "$rproc" ]; then
printf " %s has process with %%mem: %s, root %%mem: %s\n" "${userarray[i]}" "$umem" "$rmem" | tee -a "$ofn"
fi
done
printf "\n"
exit 0
<强>输出:强>
alchemy:~/scr/tmp/stack> bash procmem.sh
user nprocs maxmem
avahi 1 0.0
david 68 18.7
man 2 0.0
messagebus 1 0.0
nscd 1 0.0
ntp 1 0.0
polkitd 1 0.5
postfix 2 0.0
root 105 2.8
Users with nprocs less than root and %mem at least as great as root
david has process with %mem: 18.7, root %mem: 2.8
输出文件:
alchemy:~/scr/tmp/stack> cat userpctmem.txt
david has process with %mem: 18.7, root %mem: 2.8
注意:检索具有最大%MEM的流程的PID和PPID留待练习。如果您在尝试后遇到困难,请发表评论,然后我会告诉您一种捕捉它的方法。