给出的是一台使用 Ubuntu 的 PC。如何通过 bash 在屏幕或文件上输出 .sh 文件使用的所有终端命令的列表?运行时不需要命令的输出。我不期望给定脚本有任何输出,我只想确定脚本中使用的命令并将它们保存在要创建的新文件中。
按照 .sh 文件的示例进行操作:
#!/bin/bash
# Some comments, comments, comments
echo "######################"
echo "# FF_groesse_position"
echo "######################"
echo
echo
######################
# Variables
######################
sleep=5
######################
echo "Programm area"
######################
firefox
sleep 5
echo
echo
x=$(xdotool getdisplaygeometry | cut -d ' ' -f1 )
y=$(xdotool getdisplaygeometry | cut -d ' ' -f2 )
echo "x=$x"
echo "y=$y"
echo
echo
x=$(wmctrl -lG | grep Desktop | tr -s ' ' | cut -d ' ' -f5 )
y=$(wmctrl -lG | grep Desktop | tr -s ' ' | cut -d ' ' -f6 )
echo "x=$x"
echo "y=$y"
echo
echo
read -p "Press Enter or Ctl + C"
Run Code Online (Sandbox Code Playgroud)
想要的输出:
echo
echo
echo
echo
echo
echo
firefox
sleep
echo
echo
xdotool
cut
xdotool
cut
echo
echo
echo
echo
wmctrl
grep
tr
cut
wmctrl
grep
tr
cut
echo
echo
echo
read
Run Code Online (Sandbox Code Playgroud)
也许,可以通过命令 compgen 获取 .sh 文件使用的命令列表,如下所示:
compgen -c file.sh > found_commands.txt
Run Code Online (Sandbox Code Playgroud)
或者通过类似以下内容:
set -option file.sh > found_commands.txt
Run Code Online (Sandbox Code Playgroud)
或者通过 bash 以其他方式。
询问的答案如下:
command -option file.sh > found_commands.txt
Run Code Online (Sandbox Code Playgroud)
set -x您可以在脚本中使用,并将其与将错误流重定向2>到文件相结合。
因此,在脚本的开头,使用:
#!/bin/bash -x
Run Code Online (Sandbox Code Playgroud)
或者
#!/bin/bash
set -x
Run Code Online (Sandbox Code Playgroud)
然后,像这样运行你的脚本:
./file.sh 2> found_commands.txt
Run Code Online (Sandbox Code Playgroud)
对于您的脚本,生成的文件将是:
+ echo "######################"
+ echo "# FF_groesse_position"
+ echo "######################"
+ echo
+ echo
+ sleep=5
+ echo "Programm area"
+ firefox
+ sleep 5
+ echo
+ echo
++ xdotool getdisplaygeometry
++ cut -d ' ' -f1
+ x=$(xdotool getdisplaygeometry | cut -d ' ' -f1 )
++ xdotool getdisplaygeometry
++ cut -d ' ' -f1
+ y=$(xdotool getdisplaygeometry | cut -d ' ' -f2 )
+ echo "x=$x"
+ echo "y=$y"
+ echo
+ echo
++ wmctrl -lG
++ grep Desktop
++ tr -s ' '
++ cut -d ' ' -f5
+ x=$(wmctrl -lG | grep Desktop | tr -s ' ' | cut -d ' ' -f5 )
++ wmctrl -lG
++ grep Desktop
++ tr -s ' '
++ cut -d ' ' -f5
+ y=$(wmctrl -lG | grep Desktop | tr -s ' ' | cut -d ' ' -f6 )
+ echo "x=$x"
+ echo "y=$y"
+ echo
+ echo
+ read -p "Press Enter or Ctl + C"
Run Code Online (Sandbox Code Playgroud)
然后,您可以处理输出文件,并使用以下正则表达式*删除所有变量定义
egrep -v "^\++\ ([a-zA-Z])\w*\=" found_commands.txt
Run Code Online (Sandbox Code Playgroud)
导致:
+ echo "######################"
+ echo "# FF_groesse_position"
+ echo "######################"
+ echo
+ echo
+ echo "Programm area"
+ firefox
+ sleep 5
+ echo
+ echo
++ xdotool getdisplaygeometry
++ cut -d ' ' -f1
++ xdotool getdisplaygeometry
++ cut -d ' ' -f1
+ echo "x=$x"
+ echo "y=$y"
+ echo
+ echo
++ wmctrl -lG
++ grep Desktop
++ tr -s ' '
++ cut -d ' ' -f5
++ wmctrl -lG
++ grep Desktop
++ tr -s ' '
++ cut -d ' ' -f5
+ echo "x=$x"
+ echo "y=$y"
+ echo
+ echo
+ read -p "Press Enter or Ctl + C"
Run Code Online (Sandbox Code Playgroud)
最后,您可以仅提取每行的第二个单词
egrep -v "^\++\ ([a-zA-Z])\w*\=" found_commands.txt | cut -d ' ' -f2 }'
Run Code Online (Sandbox Code Playgroud)
导致:
echo
echo
echo
echo
echo
echo
firefox
sleep
echo
echo
xdotool
cut
xdotool
cut
echo
echo
echo
echo
wmctrl
grep
tr
cut
wmctrl
grep
tr
cut
echo
echo
echo
echo
read
Run Code Online (Sandbox Code Playgroud)
您可以使用 Bash 子 shell 和重定向将其组合成单个命令,如下所示:
./file.sh 2> >(egrep -v "^\++\ ([a-zA-Z])\w*\=" | cut -d ' ' -f2 > found_commands.txt)
Run Code Online (Sandbox Code Playgroud)
现在该文件found_commands.txt将具有处理后的输出。
如果您不想重复,只想知道运行了哪些命令,请使用sort-u(唯一选项)进行过滤
./file.sh 2> >(egrep -v "^\++\ ([a-zA-Z])\w*\=" | cut -d ' ' -f2 | sort -u > found_commands.txt)
Run Code Online (Sandbox Code Playgroud)
生成脚本的“依赖项”的字母顺序列表:
cut
echo
firefox
grep
read
sleep
tr
wmctrl
xdotool
Run Code Online (Sandbox Code Playgroud)
*正则表达式细分
^行首\+加号+1 个或多个前面的元素\ 空间(开始组[a-z][A-Z]任意英文字母)端基\w单词字符 ( a-z, A-Z,0-9和_)*0 个或多个前面的元素\=等号| 归档时间: |
|
| 查看次数: |
330 次 |
| 最近记录: |