【发布时间】:2021-10-11 17:23:49
【问题描述】:
在以下脚本中(表单here)
#!/bin/sh
# Using external pipe with st, give a dmenu prompt of recent commands,
# allowing the user to copy the output of one.
# xclip required for this script.
# By Jaywalker and Luke
tmpfile=$(mktemp /tmp/st-cmd-output.XXXXXX)
trap 'rm "$tmpfile"' 0 1 15
sed -n "w $tmpfile"
sed -i 's/\x0//g' "$tmpfile"
ps1="$(grep "\S" "$tmpfile" | tail -n 1 | sed 's/^\s*//' | cut -d' ' -f1)"
chosen="$(grep -F "$ps1" "$tmpfile" | sed '$ d' | tac | dmenu -p "Copy which command's output?" -i -l 10 | sed 's/[^^]/[&]/g; s/\^/\\^/g')"
eps1="$(echo "$ps1" | sed 's/[^^]/[&]/g; s/\^/\\^/g')"
awk "/^$chosen$/{p=1;print;next} p&&/$eps1/{p=0};p" "$tmpfile" | xclip -selection clipboard
为什么脚本卡在sed -n "w $tmpfile"
我在sed 手册页中查找了w 命令,上面没有模式也没有输入文件。
它曾经在我的机器上工作过!但不确定发生了什么以及导致它变成这样的原因......
【问题讨论】:
-
你希望这条线做什么?
-
@Shawn 不确定。可悲的是,我只是脚本的用户。不确定每个特定行的作用。这个脚本的作者在 github 上没有回应这个问题。我的机器好像出了点问题。这里很难,它似乎试图向文件中写入一些东西,但你可以看到那里没有输入文件。
-
那行
sed -n "w $tmpfile"将管道输入复制到名称存储在"$tmpfile"中的文件中。您可以改用cat > "$tmpfile"。运行tmpfile='foo'; echo 17 | sed -n "w $tmpfile"; cat foo看看它做了什么。 -
是的。它在那条线上寻找标准输入。但是这个脚本不需要用户的任何输入。
-
它需要来自某处的输入,无论是不是用户idk。