menu + awk + while + case


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers menu + awk + while + case
# 1  
Old 11-28-2011
menu + awk + while + case

the idea is to create script with menu and when option 1 or2 is pressed program should clear screan display info and get back to menu....
I managed some code but getting errors...

Code:
#!/bin/bash
choice1=ls -l|awk'{print $9 $1}'
choice2= ls | wc -c
choice3=exit

while [ $choice -ne 2 ]
do

clear

echo " ========================================= "
echo " = 1) display diles and permissions only = "
echo " = 2) calculate numbers of bytes         = "
echo " = 3) exit                               = "
echo " ========================================= "

echo -n "                     choice: "
read choice

case "$choice" in
1 ) echo " $choice1 " # how to display info and get back to menu script
begining?
2 ) echo " $choice2 " # same as with number1
3 ) exit

thanks!

Last edited by me.; 11-28-2011 at 02:35 PM..
# 2  
Old 11-28-2011
As a start:
  1. Assigning the output of a command to a variable needs the command(s) on the right side of the = either enclosed inside backticks `command` or this $(command).
  2. There may be no space between the = and the value or command of a variable.
  3. If you start a while-loop, you got to close the "do" with "done".
  4. Same for case - you got to close it with "esac".
  5. Single entries for case are ended with double semicolon ;;.

After this, try again and check your errors, read them and try to fix them. Some error messages just explain what is going wrong.
Also have a look at a scripting introduction/guide so you get some basic knowledge.
# 3  
Old 11-28-2011
yes fixed what you pointed
Code:
#!/bin/bash
choice1=$(ls -l | awk '{print $9 $1}')
choice2=$(ls | wc -c)
choice3=exit

while [ $choice -ne 2 ]
do

clear

echo " ========================================= "
echo " = 1) display diles and permissions only = "
echo " = 2) calculate numbers of bytes         = "
echo " = 3) exit                               = "
echo " ========================================= "

echo -n "                     choice: "
read choice

case "$choice" in
1 ) echo " $choice1 "
2 ) echo " $choice2 "
3 ) exit
esac

but still getting error in line 22 Smilie
I am not getting error messages regarding $choice1 $choice2 so I assume it is ok now, but that line 22 error....
# 4  
Old 11-28-2011
No, you missed setting the ;; at the end of each case option.
Also when getting error messages, start posting them so people don't have to guess around, thanks.

If you use vi for example, you can do
Code:
:set number

So you and people reading this don't have to count lines. There is also the set -x to turn debugging output on and set +x to turn it off.
# 5  
Old 11-28-2011
will try do it later, but a fix in a code would appreciate.

---------- Post updated at 06:39 PM ---------- Previous update was at 02:31 PM ----------

ok got it almost working Smilie

no errors these time but if I chose both option 1&2 there is no message displayed...
if I chose 3 - then script exits so its fine, but to display 1&2 ?

Code:
#!/bin/bash
choice=0
c1=$(ls -l|awk '{print $9 $1}')
c2=$(ls | wc -c)
c3=exit

while [ $choice -ne 3 ]
do

clear

echo " ========================================= "
echo " = 1) display diles and permissions only = "
echo " = 2) calculate numbers of bytes         = "
echo " = 3) exit                               = "
echo " ========================================= "

echo -n "       enter a number : "
read number

case $number in
1 ) echo 2 $c1 "  ;;      # these options are not shown 
2 ) echo " $c2 " ;;        # and here....
3 ) exit ;;
esac
done


Last edited by me.; 11-28-2011 at 08:11 PM..
# 6  
Old 11-29-2011
Change
Code:
1 ) echo 2 $c1 "  ;;      # these options are not shown

to
Code:
1 ) echo " $c1 "  ;;      # these options are not shown

As said already, please post an error message, if you get one, thanks. Script is working fine though the output is not very useful in my eyes.
As said already, have a look into basic shell scripting guides too, thanks. You can learn tons out of them.
# 7  
Old 11-29-2011
the "2" instead of " " " was typo, does it work for you? strange because it doesnt display output of
Code:
choice1=$(ls -l|awk '{print $9 $1}')

or
Code:
choice2=$(ls | wc -c)

it just exits when I enter " 3 "

I read online tutorials and got a Advanced Bash-Scripting Guide but I need a while with scripts to be on the level when I look at the code and find errors. Just now I am on the errors making level . . .

I am pasting full script, can you tell me if that works for as it should,
Code:
#!/bin/bash
choice=0
choice1=$(ls -l|awk '{print $9 $1}')
choice2=$(ls | wc -c)
choice3=exit

while [ $choice -ne 3 ]
do

clear

echo " ========================================= "
echo " = 1) display diles and permissions only = "
echo " = 2) calculate numbers of bytes         = "
echo " = 3) exit                               = "
echo " ========================================= "

echo -n "       enter a number : "
read choice

case "$choice" in
1 ) echo " $choice1 "  ;;
2 ) echo " $choice2 " ;;
3 ) exit ;;
esac
done

thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Gnome 3.28.3 menu item dissapears under the system menu

I installed CentOS 8 with Gnome 3.28.2 and I noticed that the "switch user" menu item disappeared from under the system menu of Gnome classic (Both X11 & Wayland). I checked google and this problem seems to have a history going back several releases of Gnome. Unfortunately, I never found a... (1 Reply)
Discussion started by: bodisha
1 Replies

2. Shell Programming and Scripting

Special case to skip function in bash menu

In the bash menu below if the variant that is inputted is in the format NM_004004.3:c.274G>T the below works perfectly. My question is if the variant inputted isNM_004004.3:-c.274G>T or NM_004004.3:+c.274G>T then the code as is will throw an error due to a biological issue. Is it possible to to... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Menu and case statement scripting

hi all i am trying to get help with writing a script using case statement to display menu as 1) Authentication log 2) System log 3) Messages 4) Dmesg 5) Boot log Q) Exit When selecting the menu by 1 or 2 or 3 o 4 or 5, it should display the last 10 lines of the log files, if... (3 Replies)
Discussion started by: renegade11
3 Replies

4. Shell Programming and Scripting

Need help in create menu with 3 sub menu using the case command

hi all i am a newbie to this is there any examples on creating a main menu with 3 sub menu main menu -> option a , b and c a menu -> option 1 ,2 and 3 b menu -> option 1 ,2 c menu -> option 1 ,2 i am getting headache as my code kept getting unexpected EOF ---------- Post... (0 Replies)
Discussion started by: chercm
0 Replies

5. Shell Programming and Scripting

Two columns output in simple case menu?

EDIT : System used : Any Linux distribution. Hello everyone, I m having quite a headache trying to figure out why I m having a 2 columns output in the following code : #!/bin/ksh menu_rotation() { #Variables CHOIX1="Rotation 1" CHOIX2="Rotation 2" CHOIX3="Rotation 3" ... (11 Replies)
Discussion started by: Sekullos
11 Replies

6. Shell Programming and Scripting

awk maintain case query

# Print list of word frequencies { $0 = tolower($0) for (i = 1; i <= NF; i++) counter++ } END { for (word in counter) printf "%s\t%d\n",word, counter } I have this simple awk code from awk user guide to count the frequency of word. Now consider the... (7 Replies)
Discussion started by: ajacobs365
7 Replies

7. Shell Programming and Scripting

case-insensitive search with AWK

Hi All, How we can perform case-insensitive search with AWK.:rolleyes: regards, Sam (11 Replies)
Discussion started by: sam25
11 Replies

8. Shell Programming and Scripting

what the awk do in this case

#!/bin/sh nowpwd=`pwd` cd $VAMPIRE_HOME configFile=$VAMPIRE_CFG/vampire.cfg inpuFileName=`awk -F\= '{ if ($1 == "PrepFilePos.InpFilePosDepo") { printf("%s",$2) exit 0 }}' < $configFile` I need to know what the awk doing here and what is the expected result here... (1 Reply)
Discussion started by: habuzahra
1 Replies

9. Shell Programming and Scripting

Clear Case, Awk and script

Hello. I should have asked this awhile ago but here is my situation. My task is to generate LOC for different directories. I have a text file that has dates in this format (01-Aug-2006). My task is to read each line and compare it to a branch date. Depending on the date, it should generate a... (0 Replies)
Discussion started by: mastachef
0 Replies

10. Shell Programming and Scripting

awk case-insensitive

can I tell awk to be case insensitive for one operation without setting the ignorecase value ? thanks, Steffen (7 Replies)
Discussion started by: forever_49ers
7 Replies
Login or Register to Ask a Question

Featured Tech Videos