while 读循环 +在 while 循环中创建一个进程

发布于 2024-09-06 00:43:41 字数 472 浏览 11 评论 0原文

下面的测试脚本有问题。当我在脚本中添加行 (sleep 5 )& 时,while read 循环不会从文件中读取所有行,而只打印第一行。

但是,当我从脚本中删除 ( sleep 5 )& 时,脚本将打印文件中定义的所有行。

为什么 ( sleep 5 )& 会导致这种情况?

以及如何解决问题呢?我想在 while 循环中创建一个新进程(其中 sleep 只是一个例子)。

$ more test

#!/bin/ksh 
while read -r line ; do 
echo $line
( sleep 5 )&   
RESULT=$!
sleep 1
kill $RESULT
done < file


$ more file
123 aaa
234 bbb
556 ccc

The following test script has a problem. When I add the line (sleep 5 )& in the script then the while read loop does not read all lines from the file, but only prints the first line.

But when I remove the ( sleep 5 )& from the script, then the script prints all lines as defined in the file.

Why the ( sleep 5 )& causes this?

And how to solve the problem? I want to create a new process (for which the sleep is just an example) in the while loop.

$ more test

#!/bin/ksh 
while read -r line ; do 
echo $line
( sleep 5 )&   
RESULT=$!
sleep 1
kill $RESULT
done < file


$ more file
123 aaa
234 bbb
556 ccc

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最舍不得你 2024-09-13 00:43:41

您提供的代码运行并打印文件中的每一行。

由于您没有等待

( sleep 5 ) &

子进程,因此它对进程执行没有影响。这是您编写的确切代码吗?

The code you provides runs and prints each line in the file.

Since you are not waiting for the

( sleep 5 ) &

child process, it has no effect on process execution. Is this the EXACT code you wrote?

谜兔 2024-09-13 00:43:41

这似乎是特定版本的 ksh 中的错误。我从 ksh 93s+ 获得了相同的效果,但从 bash、ash、pdksh、zsh 或 ksh 93t+ 获得了相同的效果。

This seems to be a bug in a specific version of ksh. I'm getting the same effect from ksh 93s+, but not from bash, ash, pdksh, zsh or ksh 93t+.

萌酱 2024-09-13 00:43:41

这个 KornShell (ksh) 脚本在这个版本中对我来说工作得很好:

  • echo $KSH_VERSION
    • @(#)MIRBSD KSH R48 2013/08/16

soExample.ksh:

#!/bin/ksh 

#Initialize Variables
file=file
fileContent="123\taaa\n234\tbbb\n556\tccc"

#Function to create File with Input
#Params: 1}Directory 2}File
createBlankFileHere(){
    echo "Entering createFileHere"
    > ${1}
    echo "Exiting createFileHere"
}

#Function to create File with Input
#Params: 1}File 2}String to write to FileName
createFileWithInputHere(){
    echo "Entering createFileWithInputHere"
    > ${1}
    #-e means 'enable interpretation of backslash escapes
    echo -e ${2} >> ${1}
    #print ${2} >> ${1}
    echo "Exiting createFileWithInputHere"
}

#Function to 
#Params: 1} File
readLine(){
    echo "Entering readLine"
    while read -r line ; do 
    echo ${line}
    ( sleep 5 )&   
    RESULT=${!}
    sleep 1
    kill ${RESULT}
    done < ${1}
    echo "Exiting readLine"
}

#-----------
#---Main----
#-----------
echo "Starting: ${PWD}/${0} with Input Parameters: {1: ${1} {2: ${2} {3: ${3}"
createFileWithInputHere ${file} ${fileContent} #function call#
readLine ${file} #function call#
echo "Exiting: ${PWD}/${0}"

soExample.ksh 输出:

user@foo /tmp
$ ksh soExample.ksh
Starting: /tmp/soExample.ksh with Input Parameters: {1:  {2:  {3:
Entering createFileWithInputHere
Exiting createFileWithInputHere
Entering readLine
123 aaa
234 bbb
556 ccc
Exiting readLine
Exiting: /tmp/soExample.ksh

This KornShell (ksh) script works fine for me with this version:

  • echo $KSH_VERSION
    • @(#)MIRBSD KSH R48 2013/08/16

soExample.ksh :

#!/bin/ksh 

#Initialize Variables
file=file
fileContent="123\taaa\n234\tbbb\n556\tccc"

#Function to create File with Input
#Params: 1}Directory 2}File
createBlankFileHere(){
    echo "Entering createFileHere"
    > ${1}
    echo "Exiting createFileHere"
}

#Function to create File with Input
#Params: 1}File 2}String to write to FileName
createFileWithInputHere(){
    echo "Entering createFileWithInputHere"
    > ${1}
    #-e means 'enable interpretation of backslash escapes
    echo -e ${2} >> ${1}
    #print ${2} >> ${1}
    echo "Exiting createFileWithInputHere"
}

#Function to 
#Params: 1} File
readLine(){
    echo "Entering readLine"
    while read -r line ; do 
    echo ${line}
    ( sleep 5 )&   
    RESULT=${!}
    sleep 1
    kill ${RESULT}
    done < ${1}
    echo "Exiting readLine"
}

#-----------
#---Main----
#-----------
echo "Starting: ${PWD}/${0} with Input Parameters: {1: ${1} {2: ${2} {3: ${3}"
createFileWithInputHere ${file} ${fileContent} #function call#
readLine ${file} #function call#
echo "Exiting: ${PWD}/${0}"

soExample.ksh Output:

user@foo /tmp
$ ksh soExample.ksh
Starting: /tmp/soExample.ksh with Input Parameters: {1:  {2:  {3:
Entering createFileWithInputHere
Exiting createFileWithInputHere
Entering readLine
123 aaa
234 bbb
556 ccc
Exiting readLine
Exiting: /tmp/soExample.ksh
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文