我试图用watch来查看一个shell脚本的输出,执行/bin/bash并在heredoc中保存脚本本身。
该鼻屎只能执行一次。 它给出正确的输出,然后观看刷新,屏幕变成空白。 退出watch ,没有列出错误。
我找不到问题出在哪里,因为用watch > bash > heredoc > ugly code难以debugging。
好消息是,heredoc中的ugly code正常工作。
将所有在Linux控制台input的命令及其结果复制到一个文件中
从文件读取行反转
在shell级别的variables范围
bash脚本从shell运行,而不是从cron作业运行
Shell脚本获得超级用户权限,而不会作为sudo运行
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 " %sn",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"
在bash中转义variables
反馈两个进程的stdin和stdout
如何获得读取/写入Python的磁盘速度?
Windows 10 Linux子系统:无法在Bash中键入Pipe(|)符号
Cygwin的bash:意想不到的EOF而寻找匹配`“'
你可以把代码放在一个函数中。 通过export -f func可以使函数定义可用于当前脚本的子进程,以便您可以这样说
watch bash -c func
在OP的例子中:
function get_user_info { #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 parentPIDs=$(ps -ef | grep "su - ${userToShow}" | grep -v 'grep|finger' | awk 'NR>1{printf " %s \|",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 | grep -v finger | awk 'root==1{print ""} NR>1{print line} {line=$0;root=($1=="root" || $3==1) ? 1 : 0}' } export -f get_user_info function show_users { [[ -z $1 ]] && watchtime=1 || watchtime=$1 [[ -z $2 ]] && export userToShow="mydefaultuser" || export userToShow=$2 echo "$mycommand" echo "Setting up watch for user ${userToShow}" watch -n $watchtime --no-title /bin/bash -c get_user_info }
watch反复运行一个指定的命令及其参数。 heredoc,更一般的重定向操作符的作用不是命令的一部分。 所以watch不能重新生成heredoc。 而且一旦heredoc被第一批bash消耗掉了,那么第二个就没有剩余了。
有一个肮脏的黑客,你可以尝试在这个答案的底部。 但我推荐的解决方案是将heredoc的内容保存在一个临时文件中。 这是相当简单的做法和强大。
将文件保存在由mktemp创建的临时文件中。 设置一个trap捕捉中断,也许其他信号,以确保临时文件得到清理。 运行watch bash "$tmpfile" 。 这很简单,将工作。
肮脏的“解决方案”(严重)的警告,不要这样做!
watch "bash -c '$var'"
或者像这样:
watch "bash -c "$var""
但是严重的警告是,如果var包含' ,第一个版本将会中断,而如果var包含' ,第二个版本将会中断。所以这些只能用于最基本的脚本,当然不是你的例子中的脚本。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。