Questions En > Help requests

Shell script - how to add options?

(1/1)

Taco.22:
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.

melodie:
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

djohnston:
One example from the modified remastersys bash script you've already seen:


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

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.


--- Code: ---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
--- Fin du code ---

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-

Navigation

[0] Index des messages

Utiliser la version classique