GoQuestion
GoQuestion

Reputation: 71

Linux Shell script executed but not return to command prompt

I have a script that runs one of the following line

sudo -u $USER $SUDOCMD &>>stdout.log

The sudo command is a realtime process that print out lots of stuff to the console.

After running the script each time, the script does not return to the command prompt. You have to press enter or ctrl + c to get back to the command prompt.

Is there a way to to do it automatically, so that I can get a return value from the script to decide whether the script runs ok or failed.

thanks.

Upvotes: 0

Views: 9843

Answers (1)

l0b0
l0b0

Reputation: 58828

What is probably happening here is that your script is printing binary data to the TTY rather than text to standard output/error, and this is hiding your prompt. You can for example try this:

$ PS1='\$ '
$ (printf "first line\nsecond line\r" > $(tty)) &>> output.log

The second command will result in two lines of output, the second one being "mixed in" with your prompt:

first line
$ cond line

As you can see the cursor is on the "c", but if you start typing the rest of the line is overwritten. What has happened here is the following:

  1. You pressed Enter to run the command, so the cursor moved a line down.
  2. The tty command prints the path to the terminal file, something like "/dev/pts/1". Writing to this file means that the output does not go to standard output (which is usually linked to the terminal) but directly to the terminal.
  3. The subshell (similar to running the command in a shell script) ensures that the first redirect isn't overridden by the second one. So the printf output goes directly to the terminal, and nothing goes to the output log.
  4. The terminal now proceeds to print the printf output, which ends in a carriage return. Carriage return moves the cursor to the start of the line you've already written to, so that is where your prompt appears.

By the way:

Upvotes: 1

Related Questions