0

My code does not work. In the if-statement it says "too many arguments". I know there are alternative ways to do this but I want to find out what is wrong with my code.

for fname in "*"
do
  h=$fname
  if [ -f $h ]
  then
    echo $fname
  fi
done
chris
  • 4,988
  • 20
  • 36
Alex_ban
  • 221
  • 2
  • 11

2 Answers2

3

Remove the double quotes around * see:

for fname in *
do
  h=$fname
  if [ -f $h ]
  then
    echo $fname
  fi
done

hope this will work.

Double quotes: Variables are expanded when enclosed in double quotes.

That's why your code is throwing the error: "too many arguments".

chris
  • 4,988
  • 20
  • 36
Avinesh
  • 535
  • 4
  • 10
0
read LOCATION
for filename in $LOCATION
do
    if [[ -d $filename ]]; then
        echo "$filename /"
    elif [[ -f $PASSED ]]; then
        echo "$filename *"
    else
        echo "$filename is not valid"
    exit 1
    fi
done
CR47
  • 843
  • 4
  • 12
  • 33