PDA

View Full Version : How do I use 'or' in a shell scipt.



dragos240
October 2nd, 2009, 08:06 PM
Hi,

I want to make a shell script that asks me if I want to:

1. Open GDM on startup
2. Nothing.

I want it to ask me that. If I press 1, gdm will start, if I press 2, it will instead, not run gdm, and I will be returned to a tty terminal.

wojox
October 2nd, 2009, 08:13 PM
You wouldn't use or. You want an if/else statement.

dragos240
October 2nd, 2009, 08:16 PM
Thanks. Could you give me an example on how to use that? I would appreciate it.

NoaHall
October 2nd, 2009, 08:32 PM
#! /bin/bash
echo "Choose"
read choice
if [ $choice == "1" ]; then
startx
elif [ $choice == "0" ]; then
exit
else
echo "choose the right one!"
fi

carnagex420x
October 2nd, 2009, 08:35 PM
#!/bin/sh
# Start GDM

GDM_START="y"

echo "Start GDM?"
read GDM_VAR

if [ $GDM_VAR = $GDM_START ]; then
clear
echo "Starting GDM"
startx

else
clear
echo "Staying at Command Line"

fi

dragos240
October 2nd, 2009, 08:38 PM
#! /bin/bash
echo "Choose"
read choice
if [ $choice == "1" ]; then
startx
elif [ $choice == "0" ]; then
exit
else
echo "choose the right one!"
fi

Thanks!

dragos240
October 2nd, 2009, 09:01 PM
It works great. I moved the script to /etc/rc.d/ and put it in /etc/rc.conf, and it works great :) I modified it a bit so it started gdm rather than X directly, and added a choice list. So I know what I'm picking. It's interesting how all this works. What is a good site for learning bash scripting, I know some things, but not too much, and it would really help in the future.

NoaHall
October 2nd, 2009, 09:09 PM
Good :) Didn't want to give it all to you, wasn't sure how you would want it to lay out - but I guessed you'd change it anyway.

carnagex420x
October 2nd, 2009, 09:51 PM
Theres a quick run down of bash scripting here.

http://www.linux.org/docs/ldp/howto/Bash-Prog-Intro-HOWTO.html