0

I need to create script who check argument, the argument is Linux command:

cat script.sh

 case "$1" in 
     cp|cd|ls ) exec "$0" "$1" ;;
             *) echo "error: not permitted" ;;
 esac

i need to add check if the first argument is exression like this :

[ -s $file ] && echo OK

so i add condition like this:

 case "$1" in 
   cp|cd|ls| "[ -s .* ] && echo OK" ) exec "$0" "$1" ;;
    *) echo "error: not permitted" ;;
   esac

but it doesn't work, any idea, please

Anandhu Raj
  • 137
  • 1
  • 6
  • 1
    No single argument has spaces in it. Are you putting the "[ -s $file ] && echo OK" argument in quotations when passing it into the script? – anerisgreat Oct 17 '19 at 04:56
  • i use a simple quote, i launch the script like this : script.sh '[ -s $file ] && echo OK' – alpha.romeo Oct 17 '19 at 05:33

1 Answers1

0

You can not place conditions into the 'case' pattern. You can place them into the command part of the case.

case "$0" in 
     cp|cd|ls )
         [ -s "$1" ] && exec "$0" "$1"
         echo "Bad parameter" ;;
     *) echo "error: not permitted" ;;
esac

Also note about "cd": You can not execute "cd /path" in a sub process.

dash-o
  • 13,723
  • 1
  • 10
  • 37
  • thanks for the reply , but i don't want to place condition in case , i want to consider the condition like a string a i want a regex who match this string : '[ -s "$fichier" ] && echo OK' – alpha.romeo Oct 18 '19 at 04:27
  • @alpha.romeo unfortunately, the case statement only works with (static) pattern matching, not with conditions. You can switch to a series if 'if'/'elif' statements. More verbose, and flexible. – dash-o Oct 18 '19 at 04:59