Shell 脚本中的代码在其他操作之前运行
Code in Shell Script runs before other Operations
This same problem is better formulated in a question posted to the Unix & Linux StackExchange community.
我正在编写一个脚本,它可以在按键时打开,打开一个新的终端(gnome-terminal),运行 scrot(截图工具),将图片以随机名称保存到一个目录,上传到 pomf.cat 并将 link 复制到剪贴板。
这很好用。我现在要做的是,上传完成后,关闭终端。
我的脚本是这样工作的:
快捷方式 (PrtScr) -> gnome-terminal -e "python path/to/script.py" -> 启动 Scrot -> 保存文件(并记住文件路径) -> bash script2.sh path/to/picture -> 上传到 pomf.cat -> 获取 link -> 通过 "xclip -selection clipboard"
放入剪贴板因为我想在将字符串放入剪贴板后关闭终端,所以我添加了这个:
eval $(printf $link | xclip -selection clipboard && sleep 1 && pkill terminal)
问题是,没有任何内容被复制到剪贴板并且终端关闭。
然而,如果没有“&& sleep 1 && pkill terminal”,link 会被复制,但终端保持打开状态。
提前致谢。
//编辑
第一个脚本(运行 scrot)
#!/usr/bin/env python
import os
import uuid
import time
def rstring(string_length=10):
random = str(uuid.uuid4())
random = random.upper()
random = random.replace("-","")
return random[0:string_length]
randomString = rstring(16)
os.system("scrot -s -q 100 /home/timon/screenshots/" + randomString + ".jpg")
while True:
processRead = os.popen("ps aux | grep \"scrot -s\" | cat").read()
if "scrot -s" not in processRead:
time.sleep(1)
else:
break
system.sleep(3)
os.system("/home/timon/.screenshot_stuff/./screen.sh /home/timon/screenshots/" + randomString + ".jpg")
第二个脚本(用于上传屏幕截图)
#!/usr/bin/env bash
dest_url='https://cuntflaps.me/upload.php'
return_url='https://a.cuntflaps.me'
if [[ -n "" ]]; then
file=""
if [ -f "${file}" ]; then
printf "Uploading ${file}..."
my_output=$(curl --silent -sf -F files[]="@${file}" "${dest_url}")
n=0 # Multipe tries
while [[ $n -le 3 ]]; do
printf "try #${n}...\n"
if [[ true ]]; then
return_file=$(echo "$my_output" | grep "url" | sed 's/\,//g' | sed 's/\//g' | sed 's/\"//g' | sed 's/\url://g' | tr -d ' ')
printf 'done.\n'
break
else
printf 'failed.\n'
((n = n +1))
fi
done
printf "$return_file" | xclip -selection clipboard && pkill terminal
else
printf 'Error! File does not exist!\n'
exit 1
fi
else
printf 'Error! You must supply a filename to upload!\n'
exit 1
fi
所以最后我想出了自己的解决方案。
问题似乎出在 xclip 本身。 现在我使用 "xsel --clipboard --input",这似乎有效,即使在直接退出后也是如此。