Auteur Sujet: Shell script - how to add options?  (Lu 4466 fois)

0 Membres et 1 Invité sur ce sujet

Hors ligne Taco.22

  • Membre Senior
  • ****
  • Messages: 391
    • Scorpio_Openbox
Shell script - how to add options?
« le: 10 février 2013 à 01:16:41 »
I have written a simple shell script with a .desktop file attached that gets the browser to open a certain page.  However the x-www-browser is not always recognised, and the browser may be changed or another added.

How do I set options in a shell script - "if this isn't found try this" sort of thing.  And/or a direction to a good online tutorial on this sort of thing.

Thanks.
What can go wrong !!!

Hors ligne melodie

  • Administrateur
  • Membre Héroïque
  • *****
  • Messages: 1777
    • Citrotux
Re : Shell script - how to add options?
« Réponse #1 le: 10 février 2013 à 01:37:22 »
I'm not good at that either, but have seen several times "if ... elif" or "if ... elsif" statements. Here two places where you can probably find a start:
http://linuxconfig.org/Bash_scripting_Tutorial#9-bash-if--else--fi-statements

http://stackoverflow.com/questions/7126752/what-is-the-difference-between-else-if-and-elif-in-bash
Good leaders being scarce, following yourself is allowed.

djohnston

  • Invité
Re : Shell script - how to add options?
« Réponse #2 le: 10 février 2013 à 05:42:20 »
One example from the modified remastersys bash script you've already seen:

if [ "$FSTYPE" = "reiserfs" ]; then
mkfs.reiserfs /dev/$TARGETPART
elif [ "$FSTYPE" = "xfs" ]; then
mkfs.xfs /dev/$TARGETPART
else
mke2fs -t $FSTYPE /dev/$TARGETPART
fi

TRANSLATION:
If the chosen filetype is reiser, then
make a reiser filesystem on the target partition
or if the chosen filetype is xfs, then
make an xfs filesystem on the target partition
otherwise
make an ext filesystem of the chosen type (2,3 or 4) on the target partition
-end of if loop-

Here's one more example, using or.

if [ "$FSTYPE" = "ext2" ] || [ "$FSTYPE" = "ext3" ] || [ "$FSTYPE" = "ext4" ]
then
echo "Using tune2fs to prevent the forced checks on boot"
tune2fs -c 0 -i 0 /dev/$TARGETPART
fi

TRANSLATION:
if the filetype is ext2 or ext3 or ext4
then
echo message to the terminal (stdout)
tune2fs with parameters to turn off filechecks based on time lapsed or number of reboots since last check
-end of if loop-

« Modifié: 10 février 2013 à 05:44:21 par djohnston »