-1

New to bash here. I am trying to pass a read input to a filepath for a cat command.

read -p "enter folder" folderid
read -p "enter port (optional)if not required press 0:" port
if [$folderid -ne 0 && $port -ne 0];then
  new_interface=$(cat /var/abc/def/xyz_$fileid/randomfile)
  echo "$new_interface"
fi

Sample input for folderid: 134567

I get an error stating:

-bash: [134567: command not found

Not sure what am I doing wrong, tried different things nothing seems to work.

Jens
  • 69,818
  • 15
  • 125
  • 179
Vbr
  • 111
  • 1
  • 7

2 Answers2

1

Add a space after the [, and the complete error free script should be

read -p "enter folder" folderid
read -p "enter port (optional)if not required press 0:" port

if [ "$folderid" -ne 0 ] && [ "$port" -ne 0 ]; then
    new_interface="$(</var/abc/def/xyz_$fileid/randomfile)"
    echo "$new_interface"
fi

If folderid is expected to be a string use

if [ ! -z "$folderid" ] && [ "$port" -ne "0" ];then

Also, remove the useless-use-of-cat in

new_interface=$(cat /var/abc/def/xyz_$fileid/randomfile)

to just

new_interface="$(</var/abc/def/xyz_$fileid/randomfile)"
Inian
  • 80,270
  • 14
  • 142
  • 161
1

The shell's tokenizer is white-space sensitive. Words are split at white-space and special characters such as ;, &, && and others. Use

if [ $folderid -ne 0 -a $port -ne 0 ]; then
Jens
  • 69,818
  • 15
  • 125
  • 179