gpt4 book ai didi

linux - 观察 bash 脚本的输出

转载 作者:太空狗 更新时间:2023-10-29 11:49:18 25 4
gpt4 key购买 nike

我正在尝试使用 watch 查看 shell 脚本的输出,执行 /bin/bash 并将脚本本身保存在 heredoc 中。

鼻屎只执行一次。它给出了正确的输出,然后 watch 刷新并且屏幕变为空白。退出 watch 时没有列出任何错误。

我无法弄清楚问题出在哪里,因为使用 watch 进行调试变得越来越困难 > bash > heredoc > 丑陋的代码

好消息是 heredoc 中的 ugly code 工作正常。

function show_users { 

[[ -z $1 ]] && watchtime=1 || watchtime=$1
[[ -z $2 ]] && export userToShow="mydefaultuser" || export userToShow=$2

echo "Setting up watch for user ${userToShow}"

watch -n $watchtime --no-title /bin/bash <<-'EOF'
#Show finger results of requested user
finger ${userToShow}

#show list of users su'd into requested user
echo "************************************************************************************"
echo "users logged in as ${userToShow}"

#get the parent PIDS of any process belonging to requested user
#into a list that can be read by grep
parentPIDs=$(ps -ef | grep "su - ${userToShow}" | grep -v 'grep\|finger' | awk 'NR>1{printf " %s \\|",parentpid}{parentpid=$3}END{printf " %s\n", parentpid}')

#get usersnames associated to those parent PIDS
parentUsers=$(ps -ef | grep "${parentPIDs}" | grep -v "grep\|${userToShow}" | awk '{print $1}' | sort | uniq)

#finger each of these users and get their full name
while IFS= read -r line ; do
printf "%s: " $line
parentName=$(finger $line | awk -F":" 'NR==1{print $3}')
echo $parentName
done <<< "${parentUsers}"

#show tree for all proceses being run by requested user up to root.
echo "************************************************************************************"
ps -ef --forest | egrep -e "sshd:|-ksh|$userToShow" | grep -v grep | awk 'root==1{print ""} NR>1{print line} {line=$0;root=($1=="root") ? 1 : 0}'
EOF
}

像这样称呼:

show_users 2 "username"

最佳答案

您可以将代码放在一个函数中。使用 export -f func 可以使函数定义对当前脚本的子进程可用,这样您就可以说

watch bash -c func

在 OP 的例子中:

# don't use bash-only (IMHO ugly) function syntax
get_user_info () {
local userToShow=$1

# No need for braces around variable names unless disambiguation is required
finger "$userToShow"

echo "lots of ugly asterisks"
echo "users logged in as ${userToShow}"

# avoid grep | grep | awk
# field specifier can probasbly be made more strict
# (match only $2 instead of $0)?
parentPIDs=$(ps -ef |
awk -v user="$userToShow" 'NR>1 && ($0 ~ "su - " user) {
printf " %s \\|",parentpid}
{parentpid=$3}
END{printf " %s\n", parentpid}')

# Would be better if parentPIDs was a proper regex
# Assumes you are looking for the PPID in column 3
parentUsers=$(ps -ef |
awk -v pids="$parentPIDs" 'parentPIDs ~$3 {print $1}' |
# prefer sort -u over sort | uniq
sort -u)

while IFS= read -r line ; do
printf "%s: " "$line"
# No need to capture output just to echo it
finger "$line" | awk -F":" 'NR==1{print $3}'
done <<< "${parentUsers}"

echo "Another ugly lot of asterisks"
# Again, the regex can probably be applied to just one field
ps -ef --forest |
awk -v re="sshd:|-ksh|$userToShow" '$0 !~ re { next }
root==1{print ""}
NR>1{print line}
{line=$0;root=($1=="root" || $3==1) ? 1 : 0}'
}

export -f get_user_info

show_users () {
# Avoid complex [[ -z ... ]]; use defaults with ${var-"value if unset"}
# Mark these as local to avoid polluting global namespace
local watchtime={$1-1}
local userToShow=${2-mydefaultuser}
# no need to export these variables

echo "$mycommand"
echo "Setting up watch for user ${userToShow}"

watch -n $watchtime --no-title bash -c get_user_info "$userToShow"
}

关于linux - 观察 bash 脚本的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47638902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com