Register
It is currently Thu Jul 24, 2014 4:40 am

Easier way to write this script besides bash?


All times are UTC - 6 hours


Post new topic Reply to topic  [ 6 posts ] 
Author Message
 PostPosted: Tue Apr 26, 2011 9:16 am   

Joined: Tue Apr 26, 2011 9:14 am
Posts: 1
Trying to make a script that will clear the screen, present the user with some enumerated choices, warn them if an invalid option is entered, and keep on cycling until they break it off.

Evaluate the arguments and terminate the script (with an appropriate message) if invalid or incomplete options are entered.


Option 1 Presents the current date in International Format :
i.e. today would be 22-MAR-2010 (note the dashes and caps).

Option 2 Show the contents of a user specified file if it exists; otherwise complain.

Option 3 Compile a specified c++ program. The program will be compile a c++ program is g++ [program name]. It will produce a file called a.out. Redirect any errors. Build an executable
If the compilation is successful, rename the a.out to the name of the source file followed by .exe. Get a confirmation by the user if you are replacing an existing file.

Option 0 terminate the script.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Had tried a bash script and got some errors and here it is

errors i got : options.shl: line 5: declare: -l: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

options.shl: line 55: syntax error near unexpected token `in'
options.shl: line 55: `as || in php; #this is the "no" part'

#!/bin/bash

ACTION=$1
#ACTION="TiMe"
declare -l ACTION # -l means lowercase
ACTION=$ACTION
WHEN=$2
ACTION_LIST="[Q]uit - [t]ime - [s]how"
echo $ACTION

if [ -z $ACTION ]; then
echo "You need to specify action"
echo "Action list: "
echo "$ACTION_LIST"
echo -n "Enter choice: " # -n means do not display the last line after (it's turned on by default)
read choice_action
if [ -z $choice_action ]; then
ACTION="quit"
else
ACTION=$choice_action
fi
fi
echo "PRE $ACTION POST"
if [ $ACTION = "time" -o $ACTION = "t" ]; then #you specify commands or "Actions" like this
DATE=`date +%d`
MONTH_ABV=`date +%b`
YEAR=`date +%Y`

MONTH_ABV_CAPS="$(echo "${MONTH_ABV}"|tr 'a-z' 'A-Z')"
echo "$DATE-$MONTH_ABV_CAPS-$YEAR"
elif [ $ACTION = "quit" -o $ACTION = "q" ]; then #you specify commands or "Actions" like this
echo "Exiting..."
exit
elif [ $ACTION = "file" -o $ACTION = "show" -o $ACTION = "f" -o $ACTION = "s" ]; then #you specify commands or "Actions" like this
ACTION_ALIAS_LONG="show file"
ACTION_ALIAS_SHORT="s"
if [ -z $WHEN ]; then # -z means does not exist
echo -e "You did not specify what to $ACTION_ALIAS_LONG\nPlease specify an option" #-e means enable backslash commands
echo "Possible choices: [Q]uit - X (Will $ACTION_ALIAS_LONG on the targeted file (X))"
echo -n "Enter choice: " # -n means do not display the last line after (it's turned on by default)
read choice
declare -l choice #-l means lowercase
choice=$choice
if [ -z $choice ]; then #-z mean does not exist
echo "You did not enter anything, assuming quit"
#do nothing
elif [ $choice = "quit" -o $choice = "q" ]; then #-o means or, also known as || in php; #this is the "no" part
echo "You entered quit, exiting"
else
WHEN=$choice
WHAT=$WHEN
cat $WHAT #cat already has a error message - Hurray!
fi
else
WHEN=$choice
WHAT=$WHEN
cat $WHAT #cat already has a error message - Hurray!
fi
else
echo "Command not found."
echo "Try again"
exit
fi
:O)


Last edited by Avatarofusa on Tue Apr 26, 2011 11:02 am, edited 1 time in total.

Top
 Profile  
 PostPosted: Tue Apr 26, 2011 10:10 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
Hi,

as you can see
Quote:
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
-l is not an option of declare in your version of bash.

Quote:
options.shl: line 55:
you're missing a comment mark. I would not write comments on the same line than command.


Top
 Profile  
 PostPosted: Mon May 02, 2011 9:43 am   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
BTW, select is often used for enumerated options.


Top
 Profile  
 PostPosted: Tue May 03, 2011 11:00 am   
Site Admin
User avatar

Joined: Sun May 15, 2005 9:36 pm
Posts: 662
Location: Des Moines, Iowa
If it were me, I would seriously look into using "case/esac" for this.... makes creating a menu of choices much simpler imho, plus if the incorrect option is given, the * is very useful.


Top
 Profile WWW  
 PostPosted: Sun Apr 22, 2012 5:21 pm   

Joined: Sun Apr 22, 2012 5:16 pm
Posts: 1
i dont think professor charles dowling wants you to look on here. Its a pretty basic script i got it done in 5 minutes and we are both in the same class and have the same professor and have the same assignment and im new to linux. This is my first linux class and let em say you are way off. Look at your scripts we made in class and take pieces from them and put them together. We learned how to do all of this


Top
 Profile  
 PostPosted: Mon Apr 23, 2012 10:31 am   
Moderator
User avatar

Joined: Wed May 03, 2006 2:05 pm
Posts: 242
Welcome to the forum, khalsa5288!

I love it when folks looking for help with homework (or tests!) get called out by professors or classmates :D

It's why we usually like to see effort on the part of the original poster, and generally try to point folks in the right direction without writing the actual code for them (as demonstrated by Crouse, Watael, and Choroba above).

I do, however, hope you realized that this thread is a year old... =P

Have fun!
-Jeo


Top
 Profile YIM  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP