LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-06-2014, 02:48 AM   #1
lolzies
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Rep: Reputation: Disabled
Smile Help with a bash script


Here it is:


Code:
#!/usr/bin/env bash


#########################################################
# calendrier - Tool for managing calendars              #
#       Author: Someone Someone S                       #
#       Version: 2.0.0                                  #
#       Usage: calendrier [ACTION] [DAY]                #
#                                                       #
#                                                       #
#                                                       #
#                                                       #
#                                                       #
#                                                       #
#                                                       #
#                                                       #
#                                                       #
#########################################################
grep -q help <<<$@ && (
        
        echo "=========================================================================================="
        echo "calendrier - Tool for managing time in a shell calendar program"
        echo "Author: Someone"
        echo "Version: 2.0.0"
        echo "Usage: calendrier [add, delete, show, urgent, now, gui] [today, tomorrow, yesterday, MM-DD-YYYY]"
        echo "==========================================================================================="
)
echo -e "\e[0;32mSetting colors\e[0m"
Color_Off='\e[0m'       # Text Reset

# Regular Colors
Black='\e[0;30m'        # Black
Red='\e[0;31m'          # Red
Green='\e[0;32m'        # Green
Yellow='\e[0;33m'       # Yellow
Blue='\e[0;34m'         # Blue
Purple='\e[0;35m'       # Purple
Cyan='\e[0;36m'         # Cyan
White='\e[0;37m'        # White

if [[ -d ~/.Calendrier ]]
        then echo -e "${Green}Found Calendrier profile directory${Color_Off}" 
else
        mkdir -p ~/.Calendrier
        echo -e "${Green}Created Calendrier dir${Color_Off}"
fi
if [ "`echo $@`" = "" ]; then
        echo -e "${GREEN}:( Missing arguments${Color_Off}"
fi

case $1 in

        add)    echo -e ${Green}Adding event...${Color_Off}
                echo -e ${Green}Assuming date to be $2 in YYYY-MM-DD${Color_Off}
                case $2 in
                        today)  set dateis=`date +%Y-%m-%d`
                                        echo Date is $dateis in YYYY-MM-DD >> ~/.Calendrier/log
                                        read -p "Is this an event, a meeting, appointment, or class? " option
                                        if [ $option == event ]; then
                                                echo -e "${Green}Ah, so it's an event!${Color_Off}"
                                                echo -ne "${Green}What event are you going to? ${Color_Off}"
                                                read whatevent
                                                echo -ne "${Green}What time? ${Color_Off}"
                                                read thetimelol
                                                echo -e "${Green}Cool :), this event will be recorded${Color_Off}"
                                                echo "$dateis at $thetimelol: $whatevent" >> ~/.Calendrier/class
                                        fi
                                        if [ $option == meeting ]; then
                                                echo -e "${Green}Ah, so it's a meeting!${Color_Off}"
                                                echo -ne "${Green}If it is a meeting, who is it with (seperate names with commas)? ${Color_Off}"
                                                read whoisitwith
                                                echo -ne "${Green}OK, what time is it at? ${Color_Off}"
                                                read whattime
                                                echo -ne "${Green}Where? ${Color_Off}"
                                                read where
                                                echo -e "${Green}Perfect! Now that you're done, I will register this in the Calendrier local database ${Color_Off}"
                                                echo "$dateis at $whattime: Meeting with $whoisitwith at $where" >> ~/.Calendrier/meeting
                                        fi
                                        if [ $option == class ]; then
                                                echo -e "${Green}Ah, so it's a class!${Color_Off}"
                                                echo -ne "${Green}OK, what time is it at? ${Color_Off}"
                                                read whattime2
                                                echo -ne "${Green}What class is it (e.g. Science, Mathematics, History)? ${Color_Off}"
                                                read whatclass
                                                echo -ne "${Green}Where? ${Color_Off}"
                                                read where2
                                                echo -e "${Green}Perfect! Now that you're done, I will register this in the Calendrier local database ${Color_Off}"
                                                echo "$dateis at $whattime2:$whatclass  at $where2" >> ~/.Calendrier/class
                                        fi


                ;;
                        tomorrow)       set dateis=`date  --date="tomorrow" +'%Y-%m-%d'`
                                        echo Date is $dateis in YYYY-MM-DD >> ~/.Calendrier/log
                                        read -p "Is this an event, a meeting, appointment, or class? " option
                                        if [ $option == event ]; then
                                                echo -e "${Green}Ah, so it's an event!${Color_Off}"
                                                echo -ne "${Green}What event are you going to? ${Color_Off}"
                                                read whatevent
                                                echo -ne "${Green}What time? ${Color_Off}"
                                                read thetimelol
                                                echo -e "${Green}Cool :), this event will be recorded${Color_Off}"
                                                echo "$dateis at $thetimelol: $whatevent" >> ~/.Calendrier/class
                                        fi
                                        if [ $option == meeting ]; then
                                                echo -e "${Green}Ah, so it's a meeting!${Color_Off}"
                                                echo -ne "${Green}If it is a meeting, who is it with (seperate names with commas)? ${Color_Off}"
                                                read whoisitwith
                                                echo -ne "${Green}OK, what time is it at? ${Color_Off}"
                                                read whattime
                                                echo -ne "${Green}Where? ${Color_Off}"
                                                read where
                                                echo -e "${Green}Perfect! Now that you're done, I will register this in the Calendrier local database ${Color_Off}"
                                                echo "$dateis at $whattime: Meeting with $whoisitwith at $where" >> ~/.Calendrier/meeting
                                        fi
                                        if [ $option == class ]; then
                                                echo -e "${Green}Ah, so it's a class!${Color_Off}"
                                                echo -ne "${Green}OK, what time is it at? ${Color_Off}"
                                                read whattime2
                                                echo -ne "${Green}What class is it (e.g. Science, Mathematics, History)? ${Color_Off}"
                                                read whatclass
                                                echo -ne "${Green}Where? ${Color_Off}"
                                                read where2
                                                echo -e "${Green}Perfect! Now that you're done, I will register this in the Calendrier local database ${Color_Off}"
                                                echo "$dateis at $whattime2:$whatclass  at $where2" >> ~/.Calendrier/class
                                        fi

;;
esac
                                        if [ "$2" != "today" ] && [ "$2" != "tomorrow" ]; then
                                                if date --date="$2" 2> /dev/null > /dev/null
                                                then set dateis="$2"
                                                echo Date is $dateis in YYYY-MM-DD >> ~/.Calendrier/log
                                        read -p "Is this an event, a meeting, appointment, or class? " option
                                        if [ $option == event ]; then
                                                dateis=$2
                                                echo -e "${Green}Ah, so it's an event!${Color_Off}"
                                                echo -ne "${Green}What event are you going to? ${Color_Off}"
                                                read whatevent
                                                echo -ne "${Green}What time? ${Color_Off}"
                                                read thetimelol
                                                echo -e "${Green}Cool :), this event will be recorded${Color_Off}"
                                                echo "$dateis at $thetimelol: $whatevent" >> ~/.Calendrier/class
                                        fi
                                        if [ $option == meeting ]; then
                                                dateis=$2
                                                echo -e "${Green}Ah, so it's a meeting!${Color_Off}"
                                                echo -ne "${Green}If it is a meeting, who is it with (seperate names with commas)? ${Color_Off}"
                                                read whoisitwith
                                                echo -ne "${Green}OK, what time is it at? ${Color_Off}"
                                                read whattime
                                                echo -ne "${Green}Where? ${Color_Off}"
                                                read where
                                                echo -e "${Green}Perfect! Now that you're done, I will register this in the Calendrier local database ${Color_Off}"
                                                echo "$dateis at $whattime: Meeting with $whoisitwith at $where" >> ~/.Calendrier/meeting
                                        fi
                                        if [ $option == class ]; then
                                                dateis=$2
                                                echo -e "${Green}Ah, so it's a class!${Color_Off}"
                                                echo -ne "${Green}OK, what time is it at? ${Color_Off}"
                                                read whattime2
                                                echo -ne "${Green}What class is it (e.g. Science, Mathematics, History)? ${Color_Off}"
                                                read whatclass
                                                echo -ne "${Green}Where? ${Color_Off}"
                                                read where2
                                                echo -e "${Green}Perfect! Now that you're done, I will register this in the Calendrier local database ${Color_Off}"
                                                echo "$dateis at $whattime2:$whatclass  at $where2" >> ~/.Calendrier/class
                                        fi
                                        else
                                                echo -e "${Green}Don't be silly, `whoami`. That's not a date. PS: It should be formatted YYYY-MM-DD or YY-MM-DD${Color_Off}"
                                        exit
                                        fi

fi

        gui)




































;;
        esac
Are there any tips (besides the ifs mess )? Any bugs?

Please don't be mean. This is my first try. Also, I am still working on it. It is NOT complete yet.

Last edited by lolzies; 07-07-2014 at 01:30 AM.
 
Old 07-06-2014, 03:01 AM   #2
ButterflyMelissa
Senior Member
 
Registered: Nov 2007
Location: Somewhere on my hard drive...
Distribution: Manjaro
Posts: 2,766
Blog Entries: 23

Rep: Reputation: 411Reputation: 411Reputation: 411Reputation: 411Reputation: 411
Hi there

Quote:
Please don't be mean. This is my first try. Also, I am still working on it. It is NOT complete yet.
Nobody is, and I should know, I asked weirder stuff and they still let me in as long as you adhere to the rules, you're okay...

So, where is the problem? You did supply the script, but...where do you reach the "bump in the road"?

Welcome to the forum!!

Thor

Edit - personally, I'd consider PERL as a next step, since all systems have that installed by default anyway, or at least have it ready to install in the respecitve repo

Last edited by ButterflyMelissa; 07-06-2014 at 03:03 AM.
 
Old 07-06-2014, 03:20 AM   #3
lolzies
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hello

Thanks for the warm welcome. Anyway, I was just looking for some tips (e.g how to make it cleaner because it looks like a mess).

lolzies
 
Old 07-06-2014, 05:06 AM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
Instead of
Code:
if ls ~/.Calendrier > /dev/null 2> /dev/null
try
Code:
 
if [[ -d ~/.Calendrier ]]
http://tldp.org/LDP/abs/html/fto.html

Incidentally, [[ ]] rather than [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS

You may find these useful
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Also, look at getopts https://stackoverflow.com/questions/...datory-options
 
1 members found this post helpful.
Old 07-06-2014, 05:42 AM   #5
lolzies
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks Chris Fixed that.

PS: Those links are very useful
 
Old 07-06-2014, 06:20 AM   #6
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
Another good bash guide

http://mywiki.wooledge.org/BashGuide



My tip, instead of echoing to stdout and piping .. try this

Code:
grep -q help <<<$@ && (
 echo "help menu"
)
<<< is heredocs ,, covered in the link above.


The && is also covered
 
1 members found this post helpful.
Old 07-07-2014, 01:28 AM   #7
lolzies
LQ Newbie
 
Registered: Jul 2014
Posts: 5

Original Poster
Rep: Reputation: Disabled
Talking Thanks everyone

Fixed everything (including the help). Keep 'em coming.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
How to end the bash script using commands in bash not manually by pressing ctrl+c Sanpreet Singh Linux - Newbie 1 07-03-2013 01:04 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration