LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2009, 06:09 AM   #1
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Rep: Reputation: 45
Novice needs Bash help.


I cannot seem to figure out why I cant execute "top" or "nano" is this test script. Feedback much welcome.

It seems my value of $C always breaks and doesn't execute my command.

Code:
#!/bin/bash
clear
echo "Testing this script"
echo
echo "1-   top"
echo "2-   nano"
echo
echo "Select either (1-2) and press Enter:"
while read C && test "$C" \!= "1" -a "$C" \!= "2"; do
  case $C in
      1)
           top   
          clear && break ;;
      2)                                                   
           nano
         clear && break ;;       
     *)
         clear && echo "That's not one of the choices, try again"
       echo ;;
  esac
done
Newbie in Training....

Last edited by manwithaplan; 02-25-2009 at 06:11 AM.
 
Old 02-25-2009, 06:26 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
what's the point of all that "test" stuff? just remove it.
 
Old 02-25-2009, 06:57 AM   #3
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
First off, I wanted to test the value of $C, and if its not equal to 1 or 2 then print an error message.

I'm very new too this... using man's and tutorials. So I'm testing 'while' loops in scripts for menus, before I put them into another script.

Then use $C variable further down the script, to wait for an input to execute a command using 'case'.


Is there an alternative to using 'test'?

Again I'm very new to this... so forgive the ignorance

Last edited by manwithaplan; 02-25-2009 at 07:01 AM.
 
Old 02-25-2009, 07:02 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
well you've already got a working test in the *) option. that's the point of the case statement, right? I've never seen additional commands between a while and a do, suprised it works at all personally, but if it did work your logic is backwards, that test will only be true if C is NOT 1 or 2, therefore those 1) and 2) clauses can logically never be reached.
 
Old 02-25-2009, 07:05 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Debian, Mint, Puppy, Raspbian
Posts: 3,421

Rep: Reputation: 200Reputation: 200Reputation: 200
Code:
select choice in top nano;
do
    echo $choice
    break
done
Code:
dos?1.sh
1) top
2) nano
#? 2
nano

Last edited by bigearsbilly; 02-25-2009 at 07:07 AM.
 
Old 02-25-2009, 07:22 AM   #6
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Your right it doesn't work... it seems test doesnt support '*' wildcards, like in a case statement.

I changed it:

Code:
#!/bin/bash
clear
echo "Testing this script"
echo
echo "1-   top"
echo "2-   nano"
echo
echo "Select either (1-2) and press Enter:"
while read C && test "$C" \!= "1" -a "$C" \!= "2"; do
   clear && echo "That's not one of the choices, try again"
done
  case $C in
      1)
           top   
          clear && break ;;
      2)                                                   
           nano
         clear && break ;;       
   esac
It seems I'm way limited to adding additional choices. e.g. 3,4,5,6,7,8,9

bigearsbilly:

Perfect, I changed it too:

Code:
  select choice in top nano;
do
  $choice
    break
done
Though I need to add a menu with extra print

Last edited by manwithaplan; 02-25-2009 at 07:26 AM.
 
Old 02-25-2009, 07:30 AM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
no no, just nuke the test statement, and use a PROPER case statement.
Code:
#!/bin/bash
clear
echo "Testing this script"
echo
echo "1-   top"
echo "2-   nano"
echo
echo "Select either (1-2) and press Enter:"
while read C ; do
  case $C in
      1)
           top   
          clear && break ;;
      2)                                                   
           nano
         clear && break ;;      
     *)
         clear && echo "That's not one of the choices, try again"
         echo ;;
   esac
done
 
Old 02-25-2009, 07:49 AM   #8
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
Quote:
no no, just nuke the test statement, and use a PROPER case statement.
Bravo, this saves alot of time. Thank you

BTW: What really is 'test' used for?

Last edited by manwithaplan; 02-25-2009 at 07:57 AM.
 
Old 02-25-2009, 08:49 AM   #9
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
test is a slightly odd tool, as it is used to evaluate statements, but normally in line with other structured commands, like if and while. test is also called by [ which allows the format of a test query to look much more like a standard logic statement:

if test $a -eq b
then
...
fi

can be changed to

if [ $a -eq b ]
then
...
fi

which probably looks more self explanatory, but here [ is actually a symbolic link to the test command itself. clever little trick basically.
 
Old 02-25-2009, 09:36 AM   #10
manwithaplan
Member
 
Registered: Nov 2008
Location: ~/
Distribution: Arch || Sidux
Posts: 393

Original Poster
Rep: Reputation: 45
acid_kewpie I was looking @ your PROPER case, and noticed that it kept creating a line in my menu as it returned and didnt clear the selected number off the screen

e.g.
Code:
  Testing this script"

    "1-   top"
    "2-   nano"
  
  "Select either (1-2) and press Enter

   1

   2

   4

   etc...
So I used the [ ] "the clever little trick" and tried this to return the menu:

Code:
#!/bin/bash
clear
C=0
while [ $C -eq 0 ]; do
echo "Testing this script"
echo
echo "1-   top"
echo "2-   nano"
echo
read -p "Select either (1-2) and press Enter:  " C
 case $C in
      1)
           top   
          clear && break ;;
      2)                                                   
           nano
         clear && break ;;      
     *)
         clear
           C=0 ;;
   esac
done
This automatically reset C=0 and returns to the menu when an incorrect choice is chosen.
 
Old 02-25-2009, 02:58 PM   #11
the_ultimate_samurai
Member
 
Registered: Jan 2006
Distribution: debian-lenny
Posts: 37

Rep: Reputation: 15
Quote:
Originally Posted by acid_kewpie View Post
test is a slightly odd tool, as it is used to evaluate statements, but normally in line with other structured commands, like if and while. test is also called by [ which allows the format of a test query to look much more like a standard logic statement:

if test $a -eq b
then
...
fi

can be changed to

if [ $a -eq b ]
then
...
fi

which probably looks more self explanatory, but here [ is actually a symbolic link to the test command itself. clever little trick basically.
...wow...

i did not know that the [ was just a link for test...

i had always used the if [...]; fi format with no idea it was the same as if test...
 
Old 02-25-2009, 03:11 PM   #12
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977Reputation: 1977
odd isn't it? And you'll find (afair) that you also must match it with ] at the end of the test binary whines for no logical reason, just neatness!
 
Old 02-25-2009, 08:29 PM   #13
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,918

Rep: Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863Reputation: 863
Believe it or not, Bash even has a formal language description. That is something I never had expected from something which seems so unlogical as bash to humans.

jlinkels
 
  


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
I am a novice mohand71 LinuxQuestions.org Member Intro 1 11-16-2008 12:43 PM
Linux novice. sjoshi LinuxQuestions.org Member Intro 1 07-21-2008 02:30 PM
A Novice User Need Help eliran SUSE / openSUSE 1 12-07-2006 09:59 PM
Novice tutorial Tut Linux - Software 1 04-28-2006 03:20 PM
I'm a Novice danbinful Mandriva 4 04-14-2005 07:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:33 PM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration