-2
PATH=/opt/omd/sites/icinga/var/TEST
FLAG="$PATH/$1_$3.flag" | tr -d \'

My $3 had single quotes which I wanted to remove, hence the above tr -d \'.

Next, I ran:

/bin/touch $FLAG

And got:

/bin/touch: missing file operand Try '/bin/touch --help' for more information. 

I have also tried

/bin/touch "$FLAG" 

But got a no such file or directory error. Can someone please guide me?

JBOSSUSER
  • 3
  • 3
  • The first step in debugging is seeing what you are actually doing. Print out the value of `$FLAG`, that should show your problem. It isn't what you think it is. Also, please don't use ALLCAPS for variable names in shell scripts. You just replaced your user's PATH variable with `/opt/omd/sites/icinga/var/TEST` in this script which means that all commands now need to be called with their full path. So, check what value `$FLAG` has and then [edit] your question to tell us. Also show us what values your `$1` and `$2` have and what you want to do with them. – terdon Jun 02 '20 at 13:14
  • Do not use `PATH` as variable name... [See](https://unix.stackexchange.com/questions/42847/are-there-naming-conventions-for-variables-in-shell-scripts) – pLumo Jun 02 '20 at 13:14
  • @terdon echo "$PFAD/$1_$3.flag" | tr -d \' output is /opt/omd/sites/icinga/var/TEST/hostaddress_servicedescription – JBOSSUSER Jun 02 '20 at 13:25
  • Please ***[edit]*** your question to add extra information, comments are hard to read, easy to miss and can be deleted without warning. – terdon Jun 02 '20 at 13:30
  • So, did you try `echo $FLAG`? Did you see what the value you are trying to `touch` is? Didn't you notice it was empty? – terdon Jun 02 '20 at 13:35
  • @terdon Ya tried it and got an error 'no such file or directory' – JBOSSUSER Jun 02 '20 at 13:41
  • No, try simply `echo "FLAG: $FLAG"` so you can see what value `FLAG` has. You will see it has no value. For the reasons explained in pLumo's answer. – terdon Jun 02 '20 at 13:59
  • [How can I assign the output of a command to a shell variable?](https://unix.stackexchange.com/q/16024/170373) – ilkkachu Jun 02 '20 at 14:18

1 Answers1

2

You have two issues:


First issue:

Your variable assignment does not work like you think it does:

FLAG="$PATH/$1_$3.flag" | tr -d \'

These are two commands separated by a pipe, meaning you send the output of the first command (the variable assignment) to the second command (tr). The second command will simply output the result. As the variable assignment has empty output, the output of tr is empty, too.

The variable assignment actually works, but as it is part of a pipe it runs in separate process and the main process including the commands afterwards (e.g. touch) can not access it.

Variable assignment including a command has to be done using command substitution:

FLAG="$(printf '%s' "$PATH/$1_$3.flag" | tr -d \')"

See also.


Second issue is that you overwrite your PATH variable:

PATH=/opt/omd/sites/icinga/var/TEST
FLAG="$PATH/$1_$3.flag" | tr -d \'

Now, tr will not work and give following error:

tr: command not found

I even get a nice additional information, but that might be bash or Ubuntu:

Command 'tr' is available in the following places
 * /bin/tr
 * /usr/bin/tr
The command could not be located because '/usr/bin:/bin' is not included in the PATH environment variable.

To fix this and also don't run into similar issues, follow the bash variable naming conventions:

path=/opt/omd/sites/icinga/var/TEST
flag="$(printf '%s' "$path/$1_$3.flag" | tr -d \')"

pLumo
  • 22,231
  • 2
  • 41
  • 66
  • Sadly, in Zsh even `$path` would cause problems, as it's magically tied with `$PATH`... – ilkkachu Jun 02 '20 at 14:17
  • That is to say, if you write a script for Zsh specifically, it uses some lowercase variables itself, one of them being `path`, a mirror of `PATH`. It doesn't exist when Zsh emulates sh, so no danger there. See: http://zsh.sourceforge.net/Doc/Release/Parameters.html#index-path – ilkkachu Jun 02 '20 at 15:25