0

In bash, I would like to conditionally echo a new line depending on whether the current line in the terminal is empty or not.

Say, for example, first.sh runs first but I don't control it and I don't know what it will print each time. Can I get second.sh to start printing always on a brand new line and don't leave any blank lines above it?

first.sh

#!/bin/bash

let "n = 1"
max=$((RANDOM%3))
while [ "$n" -le "$max" ]
do
  printf "%s" x
  let "n += 1"
done

second.sh

#!/bin/bash

#if [ not_in_first_terminal_column ]
#  echo
#fi
echo "Hola"

I want one of the following outputs

$ ./first.sh && ./second.sh
Hola
$ ./first.sh && ./second.sh
x
Hola
$ ./first.sh && ./second.sh
xx
Hola

but not

$ ./first.sh && ./second.sh

Hola
$ ./first.sh && ./second.sh
xHola
$ ./first.sh && ./second.sh
xxHola

Is it possible to do what I want? I figured using ANSI escape codes, something like in here, but I haven't found a way.

Community
  • 1
  • 1
deivid
  • 4,808
  • 2
  • 33
  • 38
  • No; in this case, the terminal is merely outputting characters as it receives them from two asynchronously scheduled processes. You need a to have the two background processes write to a third process which takes care of merging the two output streams in the manner you want, and it then writes to standard output. – chepner Aug 15 '13 at 14:50
  • The background.sh process is not under my control, it could be any process outputing to the terminal. But can't my script ask for the terminal status? Just like you can delete the current line, for example, using ansi escape sequences ([see here](http://stackoverflow.com/questions/11283625/bash-overwrite-last-terminal-line/11283685#11283685) I figured you could ask for the current column as well... – deivid Aug 15 '13 at 15:53
  • It wouldn't help you if you could. It's a classic race condition: the state could change before your next output is received by the terminal. – chepner Aug 15 '13 at 16:56

1 Answers1

0

Test your variable's value if it's not empty with -n:

[[ -n $VAR ]] && echo "$VAR"

# POSIX or Original sh compatible:

[ -n "$VAR" ] && echo "$VAR"

# With if:

if [[ -n $VAR ]]; then
   echo "$VAR"
fi

if [ -n "$VAR" ]; then
   echo "$VAR"
fi

It's actually equivalent to [[ $VAR != "" ]] or ! [ "$VAR" = "" ].

Furthermore in Bash you can test it if it's only filled with whitespaces:

shopt -s extglob  ## Place this somewhere at the start of the script

[[ $VAR == +([[:space:]]) ]] && echo "$VAR"

if [[ $VAR == +([[:space:]]) ]]; then
     echo "$VAR"
fi

Use [[:blank:]] to only match spaces and tabs and not newlines and the likes if it's more helpful.

If you want to remove empty lines from input file or pipe you can use other tools like sed:

sed -ne '/^$/!p' file or ... | sed -ne '/^[[:space:]]*$/!p'

konsolebox
  • 72,135
  • 12
  • 99
  • 105
  • Thanks a lot for your answer, but I was not asking that. My question was not very clear, so I've rewritten it again with a concrete example. Thanks again! – deivid Aug 15 '13 at 09:45