繁体   English   中英

脚本从命令行正确运行,但未嵌入到另一个脚本中

[英]Script runs correctly from command line but not embedded in another script

当我从命令行使用 encpass 时:没问题

#!/bin/sh
label=$1
. ./encpass.sh
password=$(get_secret  $label)
echo "passw $password
[dbadmin@luechdb61 scripts]$ ./secret.sh  dbllud1
passw Vxxxxxxxxxxxxxxxxxxxxxxxx
[dbadmin@luechdb61 scripts]$ ./secret.sh  dbllud2
passw Tyyyyyyyyyyyyyyyyyyyyyyyyy

密码已正确检索现在我从另一个脚本中调用它......

    usr_name=$(echo ${server_name} |cut -d ':' -f3)
        echo "handling script_name ${Scr_nme} for server_name : ${srv_name=} dbname : ${db_name} "
        . ./encpass.sh
        password=$(get_secret ${usr_name})
...

在这种情况下:我也回显了输入并且是正确的,但是 get_secret 无法识别这一点并且想要创建一个新条目,尽管该条目已正确显示

[dbadmin@luechdb61 scripts]$ ./db2Deploy.sh -s scr.sql -m deploy.lst -e d
handling script_name scr.sql for server_name : luechdb61 dbname : IEEINT
xdbllud1x   <--- echo from script x${usr_name}x
Enter dbllud1:
stty: standard input: Inappropriate ioctl for device
stty: standard input: Inappropriate ioctl for device

这可能是什么原因? 感谢所有回答最好的问候,盖伊

When you execute a command, the standard input stream of that command is taken from the keyboard and the standard output and error stream go to the screen of the terminal.

echo "Hello world!"

使用此命令,“Hello world”。 显示在屏幕上。

但是当您在$(...)中执行相同的命令时,标准输入和 output 流位于不会干扰当前 shell 连接到您的键盘和屏幕的子外壳中。 在这种情况下,标准 output 被发送到变量usr_name

usr_name=$(echo "Hello world!")

使用此命令,您不会在屏幕上看到任何内容,但如果您想在屏幕上显示此消息,则需要运行:

echo "$usr_name"

绕过此问题的一种可能方法是将 output 发送到名为/dev/tty的特殊文件。 所以发出:

usr_name=$(echo "Hello world!" > /dev/tty)

将变量$usr_name留空并向终端发送“Hello world”。 即使从子外壳执行。 也可以直接从终端读取子shell的密码:尝试:

$(get_secret ${usr_name} < /dev/tty)

据我猜测您的代码,因为您没有提供 function get_secret ()或命令get_secret的代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM