hallo!
ich habe ein shell-script, dass man per parameter füttern können soll.
parameter wir "-h" kann ich schon bearbeiten, aber sowas wie "-a /usr" bekomme ich nicht hin <img src="http://www.pl-forum.de/UltraBoard/Images/Sad.gif" border="0" align="middle">
wie kann ich im unten aufgelisteten script den sourcepath per "-s /some/path" setzten lassen?
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
#!/usr/bin/sh
for i in $*; do
if [ "$i" = "-h" -o "$i" = "--help" ]; then
echo Usage: $0 [options] [source_path]
echo -h Help, this screen
else
source_path=$i;
fi
done
</font><hr></pre></blockquote>
sh und parameter
Re: sh und parameter
habs gefunden <img src="http://www.pl-forum.de/UltraBoard/Images/Happy.gif" border="0" align="middle">
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
while [ -n "$1" ]; do
case $1 in
-h) help; # function help is called
shift 1;;
-s) source_path=$2; # source_path is set
if [ -z "$source_path" -o ! -d "$source_path" ]; then
help "source path $source_path is not a valid directory!";
fi
shift 2;;
-d) dest_path=$2; # dest_path is set
if [ -z "$dest_path" -o ! -d "$dest_path" ]; then
help "destination path $dest_path is not a valid directory!";
fi
shift 2;;
-*) help "Unknown option $1!";;
*) break;;
esac
done
</font><hr></pre></blockquote>
<blockquote><pre><font size="1" face="">code:</font><hr><font face="Courier New" size="2">
while [ -n "$1" ]; do
case $1 in
-h) help; # function help is called
shift 1;;
-s) source_path=$2; # source_path is set
if [ -z "$source_path" -o ! -d "$source_path" ]; then
help "source path $source_path is not a valid directory!";
fi
shift 2;;
-d) dest_path=$2; # dest_path is set
if [ -z "$dest_path" -o ! -d "$dest_path" ]; then
help "destination path $dest_path is not a valid directory!";
fi
shift 2;;
-*) help "Unknown option $1!";;
*) break;;
esac
done
</font><hr></pre></blockquote>
Re: sh und parameter
Da so etwas immer wieder im Shellskripten auftaucht, gibt es da auch ein eigenes Kommando für (in der bash eingebaut): getopts. In der bash-Man-Page ist das Verfahren recht gut erklärt, es lässt sich auch schön anpassen.
Viel Spass noch,
Jochen
Viel Spass noch,
Jochen