shell - 在 shell 文件中使用 xautomation

标签 shell ubuntu automation

从文件读取后,我试图在终端上输入一些东西。我为此使用 xautomation,但我不确定如何在 xautomation 中输入变量。 PFB 我的代码 -

FILENAME="sample.txt"

#set -vx
QUOTES=\'
cat $FILENAME | while read LINE
do
    set -vx

#   echo $QUOTES$STR$LINE$QUOTES
    xte 'sleep 1' \'$LINE\' 'key Return'
    #read the output and put in the output text file
done

编辑:但在终端上它给出的输出为 -
+ read LINE
+ set -vx
+ xte 'sleep 1' ''\''str' 'pwd'\''' 'key Return'

Unknown command ''str'
Unknown command 'pwd''

编辑2
1 个命令 - xte 'sleep 1' 'str pwd' 'key Return'这将给出 pwd 命令的输出。所以在运行任何代码时,我需要在它周围加上引号。

如果我做错了,请告诉我,我是 shell 编程的新手。

谢谢

最佳答案

xte 'sleep 1' "${LINE}" 'key Return'

您必须使用双引号来保留变量并使用正确的格式来调用字符串中的变量。

引用 GNU Bash reference :

Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘’, ‘\’, and, when history expansion is enabled, ‘!’. When the shell is in POSIX mode (see Bash POSIX Mode), the ‘!’ has no special meaning within double quotes, even when history expansion is enabled. The characters ‘$’ and ‘’ retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline. Within double quotes, backslashes that are followed by one of these characters are removed. Backslashes preceding characters without a special meaning are left unmodified. A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ‘!’ appearing in double quotes is escaped using a backslash. The backslash preceding the ‘!’ is not removed.

关于shell - 在 shell 文件中使用 xautomation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26104461/

相关文章:

bash - while 循环永远不会结束

json - 如何根据其他值筛选使用 jq 选择的对象?

linux - 映射到给定键盘快捷键的 linux shell 命令的名称?

linux - 从容器内将文件从主机复制到正在运行的 docker 容器?

visual-studio-2010 - 使用 PowerShell 修改 Visual Studio 解决方案和项目文件

java - Chromedriver 在 selenium web 驱动程序中打开一个空白页面而不是目标网站

sql - 使用 bash 管道而不是 select 语句

bash - Shell 脚本反斜杠和空格参数

php - Netbeans 灯 php 解释器不工作

audio - 批量MP3噪音消除