给出了一台使用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"
想要的产出:
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
也许,可以获得命令compgen使用的by .sh文件列表,如下所示:
compgen -c file.sh > found_commands.txt
或者通过喜欢以下几点:
set -option file.sh > found_commands.txt
或者在另一条路上。
所问的是以下形式的回答:
command -option file.sh > found_commands.txt
发布于 2022-12-03 23:47:03
您可以在脚本中使用set -x
,并将其与将错误流2>
重定向到文件相结合。
因此,在脚本的开头,可以使用以下任何一种:
#!/bin/bash -x
或
#!/bin/bash
set -x
然后,像这样运行您的脚本:
./file.sh 2> found_commands.txt
对于您的脚本,生成的文件是:
+ 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"
然后可以处理输出文件,并使用以下正则表达式*删除所有变量定义
egrep -v "^\++\ ([a-zA-Z])\w*\=" found_commands.txt
其结果是:
+ 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"
最后,您只能提取每一行的第二个单词。
egrep -v "^\++\ ([a-zA-Z])\w*\=" found_commands.txt | cut -d ' ' -f2 }'
其结果是:
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
您可以使用Bash子subshells和重定向将其组合成一个命令,如下所示:
./file.sh 2> >(egrep -v "^\++\ ([a-zA-Z])\w*\=" | cut -d ' ' -f2 > found_commands.txt)
现在,文件found_commands.txt
将具有已处理的输出。
如果您不想要重复的命令,并且只想知道哪些命令是运行的,那么使用sort-u
(唯一选项)过滤
./file.sh 2> >(egrep -v "^\++\ ([a-zA-Z])\w*\=" | cut -d ' ' -f2 | sort -u > found_commands.txt)
生成脚本的“依赖项”的字母列表:
cut
echo
firefox
grep
read
sleep
tr
wmctrl
xdotool
*RegEx细分
^
行首\+
加号+
1或更多前面的元素\
空间(
begin群[a-z][A-Z]
任何英文字母)
末端群\w
字字符(a-z
,A-Z
,0-9
和_
)*
0或更多的前置元素\=
等号https://askubuntu.com/questions/1436282
复制相似问题