WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: requesting suggestions for script (solved)  (Read 5184 times)

Offline vinnie

  • Hero Member
  • *****
  • Posts: 1187
  • HandMace informatic works
requesting suggestions for script (solved)
« on: March 15, 2012, 09:43:14 AM »
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 ...
« Last Edit: March 15, 2012, 08:46:39 PM by vinnie »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11667
Re: requesting suggestions for script
« Reply #1 on: March 15, 2012, 10:55:39 AM »
Hi vinnie
You want to do something like:
Code: [Select]
for i in $command_string
do
    case $i in
    your case statements
    esac
done
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.

Offline vinnie

  • Hero Member
  • *****
  • Posts: 1187
  • HandMace informatic works
Re: requesting suggestions for script
« Reply #2 on: March 15, 2012, 11:12:19 AM »
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?

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: requesting suggestions for script
« Reply #3 on: March 15, 2012, 11:27:11 AM »
case $1 in
first_string ) command;
                      shift ;;
second_string ) command2;
                      shift;;
* ) echo "$0 option $1 not recognised" ;
               exit ;;
esac

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11667
Re: requesting suggestions for script
« Reply #4 on: March 15, 2012, 11:35:33 AM »
Hi vinnie
No. Maybe this will make it clearer:
Code: [Select]
for i in $@
do
    case $i in
    n)
    your sed commands
    ;;
    e)
    your sed commands
    ;;
    etc.
    esac
done
$@ 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.
« Last Edit: March 15, 2012, 11:54:23 AM by Rich »

Offline vinnie

  • Hero Member
  • *****
  • Posts: 1187
  • HandMace informatic works
Re: requesting suggestions for script
« Reply #5 on: March 15, 2012, 08:11:59 PM »
In both cases, the script works by specifying a single argument, I can not use more arguments simultaneously:

gclark suggestion http://nopaste.info/b26b4ba75d.html
rich suggestion http://nopaste.info/15deeb263c.html

In my (first post) version work only first ad last argouments ! (The reason is that $# assumes the value of the total number of arguments, then the others do not work)


Perhaps I have not explained well (seen as I translate):
I'd need a way to put the arguments in any order, so that all work regardless of their position, there will be some standard method?

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: requesting suggestions for script
« Reply #6 on: March 15, 2012, 08:23:19 PM »
while [ "$1" != "" ] ; do
    case $1 in
        first_string ) command;
                      shift ;;
        second_string ) command2;
                      shift;;
        * ) echo "Unknown option $1"; exit ;;
   esac
done
« Last Edit: March 15, 2012, 08:28:56 PM by gerald_clark »

Offline Rich

  • Administrator
  • Hero Member
  • *****
  • Posts: 11667
Re: requesting suggestions for script
« Reply #7 on: March 15, 2012, 08:26:13 PM »
Hi vinnie
You used:
Code: [Select]
for i in $@
 do
 case $1 in
 n)
 sed -i 's/^.*tile_window_width.*$/tile_window_width    = 1024/' /usr/local/share/crawl/settings/tiles_options.txt &&
 sed -i 's/^.*tile_window_height.*$/tile_window_height    = 600/' /usr/local/share/crawl/settings/tiles_options.txt &&
It should be:
Code: [Select]
for i in $@
 do
 case $i in
 n)
 sed -i 's/^.*tile_window_width.*$/tile_window_width    = 1024/' /usr/local/share/crawl/settings/tiles_options.txt &&
 sed -i 's/^.*tile_window_height.*$/tile_window_height    = 600/' /usr/local/share/crawl/settings/tiles_options.txt &&

Offline vinnie

  • Hero Member
  • *****
  • Posts: 1187
  • HandMace informatic works
Re: requesting suggestions for script
« Reply #8 on: March 15, 2012, 08:45:18 PM »
gclark second suggestion http://nopaste.info/fc5a232f4d.html do infinite loop! it is dangerous  :P

rich second suggestion http://nopaste.info/a55e4e03fa.html seems to work flawlessly!

Thanks to both :)

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: requesting suggestions for script (solved)
« Reply #9 on: March 15, 2012, 08:57:46 PM »
It is not an infinite loop. The shift removes the option, leaving the rest for the next loop.

Offline vinnie

  • Hero Member
  • *****
  • Posts: 1187
  • HandMace informatic works
Re: requesting suggestions for script (solved)
« Reply #10 on: March 15, 2012, 09:29:58 PM »
you're right, I had badly written:
now works also your version http://nopaste.info/4fb7681479.html
it has only small problem

Code: [Select]
tc@box:~$ _crawl z start
uso: _crawl [ n ] [ e ] [ f ] [ w ] [ s | start ] [ load ] [ save ]
[ n ] configura per monitor netbook
[ e ] configura per monitor esterno
[ f ] configura in fullscreen
[ w ] configura in windows
[ s | start ] fa partire crawl
[ load ] carica i salvataggi (sovrascrive!)
[ save ] stipa i salvataggi (sovrascrive!)
gli argomenti (max4) possono essere messi in qualsiasi ordine
tc@box:~$

z is an argument of *) and therefore the explanation appear, however, the subsequent start does not work.
I think it's because of the final exit ;; , whit shift works fine: http://nopaste.info/f7c772122f.html

the solutions are equivalent :)

Offline gerald_clark

  • TinyCore Moderator
  • Hero Member
  • *****
  • Posts: 4254
Re: requesting suggestions for script (solved)
« Reply #11 on: March 15, 2012, 09:37:48 PM »
http://linux.die.net/man/1/ash

Look at the getopts builtin.

Offline vinnie

  • Hero Member
  • *****
  • Posts: 1187
  • HandMace informatic works
Re: requesting suggestions for script (solved)
« Reply #12 on: March 15, 2012, 09:48:21 PM »
Thanks clark