如何退出子shell
How to exit a Subshell
基本上,我试图退出包含循环的子shell。这是代码:
`
stop=0
( # subshell start
while true # Loop start
do
sleep 1 # Wait a second
echo 1 >> /tmp/output # Add a line to a test file
if [ $stop = 1 ]; then exit; fi # This should exit the subshell if $stop is 1
done # Loop done
) | # Do I need this pipe?
while true
do
zenity --title="Test" --ok-label="Stop" --cancel-label="Refresh" --text-info --filename=/tmp/output --font=couriernew # This opens Zenity and shows the file. It returns 0 when I click stop.
if [ "$?" = 0 ] # If Zenity returns 0, then
then
let stop=1 # This should close the subshell, and
break # This should close this loop
fi
done # This loop end
echo Done
这不起作用。它从不说完成。当我按下“停止”时,它只会关闭对话框,但会继续写入文件。
编辑:我需要能够将变量从子shell 传递给父shell。但是,我需要继续写入文件并保持 Zenity 对话框出现。我该怎么做?
当您生成子进程shell 时,它会创建当前 shell 的子进程。这意味着如果您在一个 shell 中编辑一个变量,它不会反映在另一个中,因为它们是 不同的进程 。我建议你将 subshell 发送到后台并使用 $!
获取它的 PID,然后在你准备好时使用该 PID 杀死 subshell 。看起来像这样:
( # subshell start
while true # Loop start
do
sleep 1 # Wait a second
echo 1 >> /tmp/output # Add a line to a test file
done # Loop done
) & # Send the subshell to the background
SUBSHELL_PID=$! # Get the PID of the backgrounded subshell
while true
do
zenity --title="Test" --ok-label="Stop" --cancel-label="Refresh" --text-info --filename=/tmp/output --font=couriernew # This opens Zenity and shows the file. It returns 0 when I click stop.
if [ "$?" = 0 ] # If Zenity returns 0, then
then
kill $SUBSHELL_PID # This will kill the subshell
break # This should close this loop
fi
done # This loop end
echo Done
基本上,我试图退出包含循环的子shell。这是代码: `
stop=0
( # subshell start
while true # Loop start
do
sleep 1 # Wait a second
echo 1 >> /tmp/output # Add a line to a test file
if [ $stop = 1 ]; then exit; fi # This should exit the subshell if $stop is 1
done # Loop done
) | # Do I need this pipe?
while true
do
zenity --title="Test" --ok-label="Stop" --cancel-label="Refresh" --text-info --filename=/tmp/output --font=couriernew # This opens Zenity and shows the file. It returns 0 when I click stop.
if [ "$?" = 0 ] # If Zenity returns 0, then
then
let stop=1 # This should close the subshell, and
break # This should close this loop
fi
done # This loop end
echo Done
这不起作用。它从不说完成。当我按下“停止”时,它只会关闭对话框,但会继续写入文件。
编辑:我需要能够将变量从子shell 传递给父shell。但是,我需要继续写入文件并保持 Zenity 对话框出现。我该怎么做?
当您生成子进程shell 时,它会创建当前 shell 的子进程。这意味着如果您在一个 shell 中编辑一个变量,它不会反映在另一个中,因为它们是 不同的进程 。我建议你将 subshell 发送到后台并使用 $!
获取它的 PID,然后在你准备好时使用该 PID 杀死 subshell 。看起来像这样:
( # subshell start
while true # Loop start
do
sleep 1 # Wait a second
echo 1 >> /tmp/output # Add a line to a test file
done # Loop done
) & # Send the subshell to the background
SUBSHELL_PID=$! # Get the PID of the backgrounded subshell
while true
do
zenity --title="Test" --ok-label="Stop" --cancel-label="Refresh" --text-info --filename=/tmp/output --font=couriernew # This opens Zenity and shows the file. It returns 0 when I click stop.
if [ "$?" = 0 ] # If Zenity returns 0, then
then
kill $SUBSHELL_PID # This will kill the subshell
break # This should close this loop
fi
done # This loop end
echo Done