David Custer
David Custer

Reputation: 651

linux bash if $a equals anything then $b equals 1 but if $a equals nothing then $b equals nothing

default flag will always equal either nothing or [0-9], if default flag equals [0-9] then track_number equals 1 but if track_number equals nothing then track_number equals nothing

default_flag=$(mkvmerge --identify-verbose video.mkv | awk -F'[ :]+' '$4=="subtitles"&&/default_track:1[[:space:]]/{print $3}')

For some reason this does not work:

if [ -s $default_flag ]; then track_number=""; else track_number="1"; fi

Upvotes: 2

Views: 140

Answers (2)

rici
rici

Reputation: 241861

The simplest solution is to use parameter substitution:

 track_number=${default_flag:+1}

the expression on the right-hand side precisely means: "If default_flag is unset or the empty string, then the empty string, otherwise the string 1."

Upvotes: 0

anubhava
anubhava

Reputation: 785481

This condition is the problem:

if [ -s $default_flag ];

As per man test:

   -s FILE
          FILE exists and has a size greater than zero

Here you are just checking for empty string so use -z

You can do:

[[ -z "$default_flag" ]] && track_number="" || track_number="1"

Upvotes: 3

Related Questions