"function" not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting "function" not working
# 1  
Old 05-09-2013
"function" not working

I have a bash menu script (read-based), which I'm trying to add submenus to. Long story short, for that to work I need to put part of the code into a function. However, that won't work. Shell just returns "syntax error near unexpected token `}'"

What did I do wrong?

Code:
Code:
function menu() {
while [ "$input" != "q" ]; do
        echo -e "\n$red Hello $name$exmark$normal\n
        $green 1)$normal Option 1
        $green 2)$normal Option 2
        $yellow q)$normal Quit\n "

read -p "Choose your poison:  " -n 1 input
echo -e "\n"
}

# 2  
Old 05-09-2013
A function declaration should be either:
Code:
function menu {
...
}

or:
Code:
menu() {
...
}

PS: You forgot "done" to close the while-loop.
# 3  
Old 05-09-2013
Quote:
Originally Posted by Scott
A function declaration should be either:
Code:
function menu {
...
}

or:
Code:
menu() {
...
}

PS: You forgot "done" to close the while-loop.
I tried 'function menu {', 'menu() {' and the example in my post - nothing works, same error.

Thanks for pointing out I'm missing "done" at the end - I have it in the script, but forgot to move it into the function Smilie

Any other ideas regarding the function problem?
# 4  
Old 05-09-2013
Then please post the exact code.
# 5  
Old 05-09-2013
Code:
#!/bin/bash
name=`whoami`
exmark="!"
input=""
prompt="hoose your poison: "

normal=`echo "\033[m"`
aqua=`echo "\033[36m"`
yellow=`echo "\033[33m"`
whiteOnRed=`echo "\033[41m"`
red=`echo "\033[31m"`
blue=`echo "\033[34m"`
green=`echo "\033[32m"`
boldWhite=`echo "\033[1;37m"`

function menu {
while [ "$input" != "q" ]; do
        echo -e "\n$red Hello $name$exmark$normal\n
        $green 1)$normal Option 1
        $green 2)$normal Option 2
        $yellow q)$normal Quit\n "

read -p "Choose your poison:  " -n 1 input
echo -e "\n"
}

if [ "$input" = "1" ]; then
echo ""
        read -p "Are you sure? " -n 1 input2
        if [ "$input2" = "y" ] then
                who
        if [ "$input2" = "n" ] then
                menu
        else echo "Please choose 'y' or 'n'"
fi
elif [ "$input" = "2" ]; then
        echo -e "$whiteOnRed Option 2$normal"

elif [ "$input" = "q" ]; then
echo -e "Goodbye $name\n";
break
else
echo "Wrong option"
fi
done

Please keep in mind this is a work in progress, so there might be better/smarter ways to do some of this - right now I'm just trying to get it functional, I'll beautify and smarten it up afterwards Smilie
# 6  
Old 05-09-2013
c
Code:
#!/bin/bash
name=`whoami`
exmark="!"
input=""
prompt="hoose your poison: "

normal=`echo "\033[m"`
aqua=`echo "\033[36m"`
yellow=`echo "\033[33m"`
whiteOnRed=`echo "\033[41m"`
red=`echo "\033[31m"`
blue=`echo "\033[34m"`
green=`echo "\033[32m"`
boldWhite=`echo "\033[1;37m"`

function menu {
while [ "$input" != "q" ]; do
        echo -e "\n$red Hello $name$exmark$normal\n
        $green 1)$normal Option 1
        $green 2)$normal Option 2
        $yellow q)$normal Quit\n "

read -p "Choose your poison:  " -n 1 input
echo -e "\n"
done # MISSING - place as appropriate within the function
}

if [ "$input" = "1" ]; then
echo ""
        read -p "Are you sure? " -n 1 input2
        if [ "$input2" = "y" ]; then # MISSING ;
                who
        if [ "$input2" = "n" ]; then # MISSING ;
                menu
        else echo "Please choose 'y' or 'n'"
fi
elif [ "$input" = "2" ]; then
        echo -e "$whiteOnRed Option 2$normal"

elif [ "$input" = "q" ]; then
echo -e "Goodbye $name\n";
break
else
echo "Wrong option"
fi
done # DOESN'T BELONG HERE

You can use bash -n to test the syntax of your script before you run it. It's good for pointing out these kinds of issues.
This User Gave Thanks to Scott For This Post:
# 7  
Old 05-09-2013
Oh thanks so much, I didn't realize the "done" tag could wreck it, thats why I didn't move it the first time you mentioned it.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

"find . -printf" without prepended "." path? Getting path to current working directory?

If I enter (simplified): find . -printf "%p\n" then all files in the output are prepended by a "." like ./local/share/test23.log How can achieve that a.) the leading "./" is omitted and/or b.) the full path to the current directory is inserted (enclosed by brackets and a blank)... (1 Reply)
Discussion started by: pstein
1 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Red Hat

"if" and "then" statement is not working in RedHat

Dear experts, I'm trying to write a script to calculate the usage of Log Archive in a directory, so if it gets to a point where the directory size is 60%, then send out an FYI.. email. So if then it reaches to 80%, move the logs from that directory. I have written the script as follow but... (10 Replies)
Discussion started by: Afi_Linux
10 Replies

7. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

8. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

9. Programming

How to convert the "select" function into a "poll" function

i have a program using the select function but i want to convert it to poll... how can i do this? thanks in advance... :) (1 Reply)
Discussion started by: rbolante
1 Replies
Login or Register to Ask a Question