Results 1 to 3 of 3
Hello everyone! I am a n00b to the LinuxForums, I hope one of the scripting gurus can help me, it is a very basic script and the problem is small ...
- 02-19-2009 #1Just Joined!
- Join Date
- Feb 2009
- Posts
- 9
Help with a bash script menu
Hello everyone! I am a n00b to the LinuxForums, I hope one of the scripting gurus can help me, it is a very basic script and the problem is small I just cant see it!
Synopsis:
I am writing a basic bash script menu and in this script there a few requirements.
Ensure that user enters either zero arguments or one argument after your script-name.
If that user enters zero arguments after your script-name, then your script will prompt the user for an output html file pathname. Once output file is entered it starts the menu.
If that user enters one arguments then it will start the menu.
If the user enters two arguments it prompts
"Error: Command requires zero or one argument"
"USAGE: ./qpress.bash [html_file_pathname]"
Now, when I issue my script with zero arguments it prompts me as it should, once I enter the filename it starts the menu.
*Here is the problem*
When I enter 1 argument it just clears the screen nothing happens. It should start the menu but it doesn't.
When I enter two or more arguments it prompts me with the error msg as it should.
here is what my script looks like. I know it has to do with one of the error checking statements. Someone please help me!
#!/bin/bash
if [ ! -f $2 ]
then
echo "Command requires zero or one argument" >&2
echo "USAGE:./qpres.bash [httml_file_pathname]" >&2
exit
fi
if [ $# -eq 0 ]
then
clear
read -p "Enter output file pathname: $file ">&2
else
file=1
clear
exit
fi
clear
cat <<+
*======================================*
*======================================*
--------- <-QUICK PRESS MENU-> ---------
****************************************
****************************************
1. Add Text
2. Clear all Text
3. Publish webpage
4. Exit
+
read -p "Enter a menu item (1,2,3,4): " choice
case $choice in
1) echo "Item chosen: Add Text "
;;
2) echo "Item chosen: Clear all Text "
;;
3) echo "Item chosen: Publish Webpage "
;;
4) echo "Item chosen: Exit "
exit 0
;;
*) echo "Invalid Selection: $choice." >&2
;;
esac
Thank you in advance
- 02-19-2009 #2
First off, in the future, please wrap your code in [code] tags.
Anyway, your problem lies in this section here:
Let's look at what happens here. If no arguments were given, we clear the screen and prompt for a file (note that your read will not store the chosen file into $file as I think you intend). However, if any arguments were given, we set $file to be 1, clear the screen, then exit the script.Code:if [ $# -eq 0 ] then clear read -p "Enter output file pathname: $file ">&2 else file=1 clear exit fi
You actually have 3 cases here: 0 arguments, 1 argument, or other. I suggest you look into the elif control statement in bash: it will allow you to say something like this:
I hope that helps.Code:if [ "$#" -eq 0 ]; then echo "I was given no arguments." elif [ "$#" -eq 1 ]; then echo "I was given one argument." else echo "I was given more than one argument." fiDISTRO=Arch
Registered Linux User #388732
- 02-19-2009 #3Just Joined!
- Join Date
- Feb 2009
- Posts
- 9


Reply With Quote
