似乎有很多方法可以做到这一点,包括 shell 脚本中的循环、find 和 xargs。其中哪个最好,哪个最便携?
我运行以下命令来替换当前工作目录中所有文件中使用的术语:
$ find . -type f -print0 | xargs -0 sed -i'.bup' -e's/Ms. Johnson/Mrs. Melbin/g'
Run Code Online (Sandbox Code Playgroud)
这执行了单词替换,但它也创建.bup了从未有Ms. Johnson字符串的文件的文件。
如何在不创建所有这些不必要的备份的情况下执行替换?
您在终端中键入的命令与包含在脚本中的命令之间有什么区别吗?
我试图搜索在六月、七月和八月创建的文件。我用过这个方法
ls -lrth|awk '/[Jun][Jul][Aug]/ {print}'
Run Code Online (Sandbox Code Playgroud)
但它不起作用。
我想让 bash 从一个随机的短字符串中解析/提取一个完整的 URL(并且只有 url)。
例子:
bob, the address is http://www.google.com
Run Code Online (Sandbox Code Playgroud)
或者
https://foo.com/category/example.html is up
Run Code Online (Sandbox Code Playgroud)
或者
Error 123 occurred at http://bit.ly/~1223456677878
Run Code Online (Sandbox Code Playgroud)
或者
Stats are up: https://foo1234.net/report.jpg
Run Code Online (Sandbox Code Playgroud)
我尝试使用,cat foo_output | egrep -o "https?://[\w'-\.]*\s"但这似乎不起作用。
我正在写一个脚本,但我需要一些东西,但我找不到方法来做到这一点......
我需要在后台“command1 &”中创建一个命令,然后在脚本中的某个地方我需要等待它完成,然后再执行 command2。基本上,我需要这个:
注意:每个命令都在特定目录中运行!在 while 循环结束时,我的 command1 创建了 4 个目录,其中每个目录运行特定进程,因此运行的进程总数为 4
a=1
while [$a -lt 4 ]
. command1
#Generates 1 Process
a= `export $a +1`
done
#Wait until the 4 process end and then run the command2
. command2
Run Code Online (Sandbox Code Playgroud)
我已经看到有关wait带有 pid 进程号的命令的内容,但这也不起作用。
在 bash 中$0包含脚本的名称,但在 awk 中,如果我使用以下内容创建一个名为 myscript.awk 的脚本:
#!/usr/bin/awk -f
BEGIN{ print ARGV[0] }
Run Code Online (Sandbox Code Playgroud)
并运行它,它只会打印“awk”。此外,带有 i>0 的 ARGV[i] 仅用于命令行中的脚本参数。那么,如何让它打印脚本的名称,在这种情况下是“myscript.awk”?
我有两个脚本:
running_scriptscript_one我需要获取在running_script用户名下运行的/任何实例的 PID ,然后pkill停止running_script子进程。
我们期待这样的事情:
ps -fu will | grep running_script
Run Code Online (Sandbox Code Playgroud)
找到running_script进程。但是,根据 ps 命令输出检查 PID 显示cmd为:running_script进程的“bin/bash” 。
running_script作为一个分离的进程(&操作员)运行,它开始script_one。我在开始时打印 PID-s 以与ps命令的输出进行比较。
running_script &
echo $! -- $BASHPID
Run Code Online (Sandbox Code Playgroud)
在实际用例中,我们不会有某些running_script正在运行的进程的PID 。此外,script_one可能是也可能不是与running_script父进程分离的进程。
为了练习的目的,script_one只做循环。
while [ true ]
do
echo " $0 - 35sec ..."
sleep 35
done
Run Code Online (Sandbox Code Playgroud)
然而,这只是例子。要求是获取父running_script进程的PID 。
是否有选项ps或其他命令可以为我提供脚本文件的名称和 PID?或者设置一个 …
if grep -q "?" out.txt
then
echo "working"
else
cat out.txt
fi
Run Code Online (Sandbox Code Playgroud)
基本上,如果文件“out.txt”包含“?” 文件中的任何地方我都希望它回显“工作”,并且如果文件“out.txt”不包含“?” 文件中的任何地方,然后我希望它 cat out.txt
编辑:所以这就是我在做什么。我正在尝试暴力破解 openssl 解密。
openssl enc 成功返回 0,否则返回非零。注意:您会得到误报,因为 AES/CBC 只能根据正确的填充来确定“解密是否有效”。所以文件解密了,但它不是正确的密码,所以里面会有乱码。乱码中的一个常见字符是“?”。因此,如果输出包含“?”,我希望 do 循环继续运行。
这是我的 git 链接https://github.com/Raphaeangelo/OpenSSLCracker 这是脚本
while read line
do
openssl aes-256-cbc -d -a -in $1 -pass pass:$line -out out.txt 2>out.txt >/dev/null && printf "==================================================\n"
if grep -q "?" out.txt
then
:
else
cat out.txt &&
printf "\n==================================================" &&
printfn"\npassword is $line\n" &&
read -p "press return key to continue..." < /dev/tty;
fi …Run Code Online (Sandbox Code Playgroud) 我正在尝试在脚本中执行这些操作。我必须在远程主机上运行一些命令。目前,我正在这样做:
ssh root@host 'bash -s' < command1
ssh root@host 'bash -s' < command2
ssh root@host 'bash -s' < command3
Run Code Online (Sandbox Code Playgroud)
但是,这意味着我必须反复连接到服务器,这会增加处理命令之间的大量时间。我正在寻找这样的东西:
varSession=$(ssh root@host 'bash -s')
varSeesion < command1
varSeesion < command2
varSeesion < command3
Run Code Online (Sandbox Code Playgroud)
同样,我需要通过脚本运行这些命令。我已经看过了,screen但我不确定它是否可以在脚本中使用。