Tiny Core Linux
Off-Topic => Off-Topic - Tiny Tux's Corner => Topic started by: vinnie 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 ...
-
Hi vinnie
You want to do something like:
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.
-
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?
-
case $1 in
first_string ) command;
shift ;;
second_string ) command2;
shift;;
* ) echo "$0 option $1 not recognised" ;
exit ;;
esac
-
Hi vinnie
No. Maybe this will make it clearer:
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.
-
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?
-
while [ "$1" != "" ] ; do
case $1 in
first_string ) command;
shift ;;
second_string ) command2;
shift;;
* ) echo "Unknown option $1"; exit ;;
esac
done
-
Hi vinnie
You used:
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:
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 &&
-
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 :)
-
It is not an infinite loop. The shift removes the option, leaving the rest for the next loop.
-
you're right, I had badly written:
now works also your version http://nopaste.info/4fb7681479.html
it has only small problem
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 :)
-
http://linux.die.net/man/1/ash
Look at the getopts builtin.
-
Thanks clark