Problem with bash if

Hi, i am making a little script that when its invoked with p or f, it will do different things (p will ask the user to input an string and it will count its words , and f will ask the user a directory and will list the number of files in that directory. But if wont get the comparation string right:
opcion=$1
if [ "$opcion"="p" ]
then
echo $opcion
echo "Escribe una cadena"
read cadena | wc -w

fi

if [ $opcion=f ]

it always go to the first and the second if, no matter if the option is f or p.... any ideas?

space matter !

if [ "$opcion" = "p" ] <---good
if [ "$opcion"="p" ] <---bad

you might want to use a case statement instead of if

case $1 in
p)
echo $1
echo "Escribe una cadena" 
read cadena
echo "$cadena" | wc -w
;;
f)
...
;;
*)
echo "wrong argument"
;;
esac
1 Like