【问题标题】:ksh cannot export variables out of while loopksh 无法从 while 循环中导出变量
【发布时间】:2021-08-10 16:37:38
【问题描述】:

我很难从 ksh 脚本的 while 循环中读取变量“tt”。看例子,在while循环中tt为空

#!/bin/ksh
cat file | while read site

  do
  export  tt=10
  echo "tt1 is ${tt}"
  done
echo "tt2" is ${tt}

当我运行脚本时,我得到了:

tt1 is 10
tt2 is

我读过很多类似的例子,但没有一个有效。 1.我不能使用bash,2.我必须从while循环中读取$tt。

谢谢。

【问题讨论】:

  • 当我将cat file 替换为printf "%s\n" 1 2 3 4 5 并将代码粘贴到tutorialspoint.com/execute_ksh_online.php 时,我的tt2 的值为10。所以我无法重现您的问题。也许在循环之前添加export tt 会有所帮助。

标签: while-loop ksh


【解决方案1】:

试试这个代码

问题是cat ... | while read ...创建子进程

#! /bin/ksh

while read site; do
    export  tt=10
    echo "tt1 is ${tt}"
done < file

echo "tt2" is ${tt}

【讨论】:

    猜你喜欢
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-01
    • 2019-11-07
    • 2020-01-03
    相关资源
    最近更新 更多