linux - 在 bash 循环中读取响应

标签 linux bash shell

我要用 bash 编写脚本,我想从另一个应用程序读取输出,并要求用户采取行动。

psql -c "SELECT userid, name  FROM my_table"|
while read userid name
do 
    do_something  $userid $name
done

在函数 do_something 内部我想问“你确定吗等”。但这对我不起作用。

最佳答案

read output from another application

ls -1 |读我时;做 echo "$i";完成

ls -1,类似输出
| 重定向输出
read ,获取输出并放入 i
"$i",现在可以打印了


现在,在你的 while 中你可以做任何事情,比如调用一个函数或其他东西


阅读:
阅读 --help


作为脚本:

for i in $(ls -1)
do
    echo -n "Are you sure (y/n)?" && read ans
    if  [ $ans == "y" ];then
        echo $i
    fi

done

关于linux - 在 bash 循环中读取响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38460840/

相关文章:

查找二元组的 Shell 脚本

C 命令行界面 - 类似于 BASH 中的历史功能

linux - 在 Shell 脚本中使用动态变量生成 JSON

linux - Shell编码遇到段错误

c - fork,Linux 中的 execlp

linux - Perl,对存储在哈希中的用户使用 linux 命令

Bash 测试参数是否存在

c - 在子进程和父进程之间传递信息 - linux(不带 `pipe()` )

linux - 如何使用 CUPS(lp 命令)打印多页 tiff 文件?

linux - 命令在 linux 终端中有效,但在 shell 脚本中无效