Nick Kavanagh
Nick Kavanagh

Reputation: 13

Bash script not exiting after completion

First, disclaimer: I am very new at this.

I am trying to write a simple bash script for an assignment. I need to print the output of date, finger, and uptime to stdout, and to a file. From what I'm reading, the failure to exit seems to be because the processes are running in a subshell, but from the reading, I don't really understand how to do it. I found that using a pipe and input redirection via tee gives me the result I want, but it will not exit. Besides a pipe, and especially typing: | tee Time_Script_Output.txt on each line seems very inefficient, and I'm looking for a cleaner way to write the script.

code:

#! /bin/bash

bash 
/bin/date | tee Time_Script_Output.txt &
/usr/bin/finger | tee Time_Script_Output.txt &
/usr/bin/uptime | tee Time_Script_Output.txt 

exit

Thanks!, Nick Kavanagh

Upvotes: 1

Views: 4253

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80931

That first bash line is incorrect. Assuming you are running this script by hand (bash script.sh) you are entering a new bash session each time the script runs. See the output from echo $SHLVL to see how deep you've gone (also the output from ps faxww). Remove that line.

You likely also don't want to be running the first two pipelines in the background you want the commands to run and the output to be written before the next command runs or you may get ordering problems in the output.

You also don't need the explicit exit at the end. The script will exit when it hits the end automatically.

You can use {} grouping to use the pipe just once:

#!/bin/bash

{
/bin/date
/usr/bin/finger
/usr/bin/uptime
} | tee Time_Script_Output.txt 

Upvotes: 2

Related Questions