0

I keep getting ambiguous redirect in bash script.

I'm new, so if someone could explain why I'm getting this with my following code, that would be great.

Here's my code!

$input = [user inputs an email]
$variable=$(date +"%Y-%m-%d-%H:%M")
$mem_list=/root/Desktop/Dan/Logs/member-name-file" "$variable.txt

Here's the code that's getting ambiguous (there's a couple of these, but same problem.

if [[ -f $mem_list ]]          #check if file exists already
then
    echo $input >> $mem_list   #if file is already there, just append input to the file
else 
    echo $input > $mem_list    #if file is not found, make a new one [Error is here!!]
fi

Help me kindly, thanks!

Dan
  • 358
  • 2
  • 3
  • 17
  • Also see [link](http://stackoverflow.com/questions/2462385/getting-an-ambiguous-redirect-error) –  May 05 '16 at 23:52

2 Answers2

3

Because $mem_list contains a space, you have to quote it; e.g.

if [[ -f "$mem_list" ]]
    echo $input >> "$mem_list"
ensc
  • 6,704
  • 14
  • 22
  • You should also put `"$input"` in double quotes, and generally any variable interpolation where word splitting and wildcard expansion is not specifically what you require. – tripleee Mar 27 '14 at 17:49
  • ack; the whole `echo` is bad for giving out user input because it does not support the `--` delimiter (--> try to give out 'input=-e'). Writing `printf "%s" "$input" >> "$mem_list"` would be more reliable. – ensc Mar 27 '14 at 17:59
  • the funny thing, and my main question, is that it works with >> but not with > (without quotes). – Dan Mar 27 '14 at 18:00
0

Your variable assignments are wrong too. The lvalue has no $ sign and there must not be spaces around =.

mem_list="/root/Desktop/Dan/Logs/member-name-file $variable.txt"

What do you mean with [user inputs an email]? My bash 4.2 is a bit baffled...

SzG
  • 12,333
  • 4
  • 28
  • 41
  • by [user inputs an email], I meant that in my whole program, user inputs email, one by one, which gets saved onto a textfile – Dan Mar 27 '14 at 18:00