Off-Topic > Off-Topic - Tiny Tux's Corner

requesting suggestions for script (solved)

(1/3) > >>

vinnie:
I made a script that simply editing a text file and saves it, I wanted to make that the arguments worked regardless of the position entered.
ex: command_name load edit == command_name edit load
I do not know how to do it though, I tried using case/esac and if/fi, however, does not work well, would you help me?

http://nopaste.info/06e4722ddd.html

P.s. seems long but it's just that I repeated all the action for each case $1,  case $2 ...

Rich:
Hi vinnie
You want to do something like:

--- Code: ---for i in $command_string
do
    case $i in
    your case statements
    esac
done
--- End code ---
Where command_string is the command that was used to invoke the script. I don't do much scripting so my
syntax may be off, but the logic you are looking for is correct.

vinnie:
For example:

for i in $first_string
do
case $1 in
first_string) command;;
case $2 in
second_string) command;;
done

for i in $second_string
case $1 in
first_string) command;;
case $2 in
second_string) command;;
done
...

It is correct?

gerald_clark:
case $1 in
first_string ) command;
                      shift ;;
second_string ) command2;
                      shift;;
* ) echo "$0 option $1 not recognised" ;
               exit ;;
esac

Rich:
Hi vinnie
No. Maybe this will make it clearer:

--- Code: ---for i in $@
do
    case $i in
    n)
    your sed commands
    ;;
    e)
    your sed commands
    ;;
    etc.
    esac
done
--- End code ---
$@ contains $1 $2 $3 ......
The for statement will run each word in $@ through that one case statement. You can remove the other copies of
the case statement from the file.

           [EDIT]: Edited for clarity and made corrections.

Navigation

[0] Message Index

[#] Next page

Go to full version