gpt4 book ai didi

shell - 在 shell 文件中使用 xautomation

转载 作者:行者123 更新时间:2023-12-04 18:47:29 26 4
gpt4 key购买 nike

从文件读取后,我试图在终端上输入一些东西。我为此使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com