-1
elif [ "$arg" == "--file" ]  || [ "$arg" == "-f" ] && [[ read var ]]
    then 
        touch $var

I'm writing a bash script which takes in a command-line input, either long-form or short-form along with the file name to create an empty file with touch command. the above snippet is what I tried to do, but there's an error unary "read: unary operator expected".please help

R0han
  • 9
  • 2

2 Answers2

0

This happens for most commands:

$ [[ echo "hello world" ]]
bash: conditional binary operator expected
bash: syntax error near `world"'

This is because [[ .. ]] should be used to compare values, and not to run commands. To run a command, don't wrap it in anything:

$ echo "hello world"
hello world

Applied to your example:

echo "You are expected to type in a value, but you will receive no prompt."
arg="-f"
if [ "$arg" == "--file" ]  || [ "$arg" == "-f" ] && read var
then
  echo "You entered: $var"
fi
that other guy
  • 116,971
  • 11
  • 170
  • 194
  • That will always evaluate to true though as the return value of `read` is `0` unless something goes wrong (no input is still `0`). This is no better than putting it in the body of the if statement. – Andria Jun 11 '20 at 20:18
  • @ChrisBrownie55 No input (end-of-file) will make `read` exit with 1. – that other guy Jun 11 '20 at 20:21
  • This works, but I wanted it to work as the touch command which takes the filename input as command-line arguments – R0han Jun 11 '20 at 20:21
  • @thatotherguy True but that still means that the only way to test if the user actually put input in is to write another condition. It seems like they're trying to test the input not see if they hit +D or not. – Andria Jun 11 '20 at 20:24
  • @R0han If you don't actually want `read` commands in if clauses, then [How do I parse command line arguments in Bash?](https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash) might have been a better question – that other guy Jun 11 '20 at 20:27
-1

Bash needs to know that it's running a whole command

To make bash aware that it's running a command you can use the backtick syntax (not recommended) or the preferred $() command substitution syntax. Without this syntax, bash is assuming that you're putting two separate strings inside of that condition.

The error you're getting is saying that you are trying to compare two strings without an operator to do so (i.e. -eq or ==).

Here is an example of how to make it recognize your commands:

elif [[ ... ]] && [[ $(read var) ]]
then

However, this won't work. This will evaluate to false. This is because you haven't printed anything out and as such an empty string ("") is falsey.

echo your variable to test its value

elif [[ ... ]] && [[ $(read var; echo $var) ]]
then

This will read into the variable and then test if the user has typed anything into it. If the user doesn't type anything, it will evaluate to false, otherwise, it will evaluate to true and run the body of the elif statement.

Andria
  • 4,712
  • 2
  • 22
  • 38