I have finished my bash script but I am still getting two errors. I've also searched for related issues but none worked:


avem_element () {
  local e
  for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
  return 1

# cel de al doilea fisier este dat ca si cel de al doilea argument
while read linie
    echo "L-am citit pe  $linie"
done < $2
# acum procesam primul fisier
spatiu=" "
virgula=", "
while read linie
    echo "Am citit  $linie"
    # verificam daca elementul citit este un nume
    if [ avem_element "$sir1[i]" "${sir[@]}" -eq 0 ];then
done < $2

These are the errors:

./script.sh: line 16: $2: ambiguous redirect

./script.sh: line 32: $2: ambiguous redirect

Why am I getting this redirect error and how can I fix it ?

  • 1
    You should always quote variables: done < "$2". What is the value? It seems you don't check it. test -z "$2" || ! test -f "$2" && exit 1 Feb 4 '15 at 23:26
  • Thank you for the edit and for the advice. After I quote those variables it tells me No such file or directory. I am trying to "play" with two txt files and output something in another .txt. The variables represents the name of the files Feb 4 '15 at 23:36
  • 1
    The script needs two arguments so ./script.sh file1 file2 ... is this how you're invoking it ? Then your next problem is at line 26 [: too many arguments... Feb 4 '15 at 23:59
  • You're right. I was invoking it in the wrong way and I am also getting that error. How can I get rid of it ? Feb 5 '15 at 0:04

redirection error

The problem is that the file you try to read from does not exist because the variable you use to refer to the file is empty.

$2 means the second parameter but in ./script.sh there is no parameter at all. You need something like ./script.sh file1 file2

if error

The line

if [ avem_element "$sir1[i]" "${sir[@]}" -eq 0 ];then

is wrong. If I understand it correctly then you want to call the function avem_element with two parameters and compare the exit code. But [ ] compares strings only. if checks an exit code (often the one of [ ]). You don't need [ ] at all. Just do this:

if avem_element "$sir1[i]" "${sir[@]}"; then
  • I have two files created under the same directory. There's also the script that should output the result in another file. Feb 4 '15 at 23:54
  • @Alex How do you call that script? Feb 4 '15 at 23:58
  • I did: chmod +x then I just did: ./script.sh Feb 4 '15 at 23:59
  • @Alex $2 means the second parameter but in ./script.sh there is no parameter at all. You need something like ./script.sh file1 file2. Feb 5 '15 at 0:08
  • Yes. I figured it out @Hauke. Look at the upper comments. There is another error. Feb 5 '15 at 0:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.