LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 04-28-2011, 09:44 PM   #1
CygwinU
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Rep: Reputation: 0
Bash Script Help PLEASE


checking is $num is an integer

read $num

if [[ $num =~ ^[0-9] ]]; then
echo "is integer"
else echo "not interger"
fi

What wrong with this please help??
 
Old 04-28-2011, 10:14 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Hi, welcome to LQ!

Quote:
Originally Posted by CygwinU View Post
checking is $num is an integer

Code:
read $num

if [[ $num =~ ^[0-9] ]]; then
echo "is integer" 
else echo "not interger"
fi
What wrong with this please help??
The red bit is wrong ...

That said, the regex is wrong, too, because it
would flag 12abc as an integer... you really want
something like ^[0-9]+$


Cheers,
Tink

Last edited by Tinkster; 04-28-2011 at 10:33 PM. Reason: italics
 
Old 04-28-2011, 10:29 PM   #3
athrin
Member
 
Registered: Mar 2011
Posts: 135

Rep: Reputation: 1
Quote:
read $num
yeah the $ is wrong.. should't be there
 
0 members found this post helpful.
Old 04-29-2011, 04:45 AM   #4
CygwinU
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks

Thanks so much guys!!

This is due in about 6hours and i need to make it perfect. Please help me on this again guys.
My teacher has a program that will be run to test my program. Therefor i can't use read to make the user input the arguments.
Can you please help me and also see what else i can do to make this better. Thanks Guys.
Ohh and we are limited to using the following:
1) test
2) printf
3) echo
4) sleep
5) expr
6) exit
7) basename
8) readonly
9) grep

im so screwed arnt i?

---------- Post added 04-29-11 at 06:46 PM ----------

Code:
#################################################################### 
#                                                                  # 
# Program: a1                                                      # 
#                                                                  # 
# Date: 08/04/11                                                   #
# Programmer: Justin Kaese                                         #
#                                                                  # 
# This program will take command line arguments -s NN where NN     # 
# is the delay in whole seconds and in the range of 1 <= NN <= 60. # 
#                                                                  # 
# It will then display the date in the format dd-mm-yyyy           # 
# ten times NN seconds apart.                                      #
#                                                                  # 
####################################################################


################################################
# Assume that the user will enter 'a1' for inp1
################################################

read inp1 inp2 inp3

   ############################################
   # see if there are enough command arguments
   ############################################ 

   if [ "$inp2" == "" ];   
   then

   echo "ERROR:not enough command line arguments
   Usage: a1 -s NN, where NN is in the range 1 - 60 inclusive
   exit code from program is 1"
   else

   if [ "$inp3" == "" ];
   then 

   echo "ERROR:not enough command line arguments
   Usage: a1 -s NN, where NN is in the range 1 - 60 inclusive
   exit code from program is 1"
   else

      ########################
      # Make sure inp2 = "-s" 
      ########################

      if [ "$inp2" == "-s" ];
      then

         #################################
	      # make sure inp 3 is an interger	
         #################################

			if [[ $inp3 =~ ^[0-9]+$ ]];
			then

            ###########################################
            # make sure inp3 is in the rage of 1 to 60
		   	###########################################

            if [ $inp3 -gt 0 ] && [ $inp3 -le 60 ];
            then 

               ############################
               # Create a loop for input 2
               ############################

			      for i in {1..10};

               ################################
               # Print the date in this format
               ################################

               do printf "%2s) " $i & date +%d-%m-%Y": "%H-%M-%S;
			
			      ####################
               # Sleep for input 3
               ####################

               sleep $inp3
               done

               echo " "
               echo "exit code from program is 0"

            else	 
            echo "ERROR:number of seconds is outside reqired range 
            Usage: a1 -s NN, where NN is the range 1 - 60 inclusive
            exit code from program is 5"
            fi

         else echo "ERROR:seconds not an integer
			Usage: a1 -s NN, where NN is in the range 1 - 60 inclusive
			exit code from program is 4"
			fi

      else echo "ERROR: did not supply the -s option
      Usage: a1 -s NN, where NN is in the range 1 - 60 inclusive
      exit code from program is 5"
      fi
fi
exit;
fi

Last edited by CygwinU; 04-30-2011 at 08:58 PM. Reason: Put my code in code tags
 
Old 04-29-2011, 05:32 AM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
When posting code wrap it in CODE tags.


bash scripts will take arguments passed from the command line.
See section "3.2.5. Special parameters" at
http://tldp.org/LDP/Bash-Beginners-G...tml#sect_03_02
 
Old 04-29-2011, 09:14 AM   #6
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Welcome to LQ. Hope your time here helps you as much as mine has helped me.

Amplifying arizonagroovejet's (righteous) request:
Please put code, command line output, config files, etc. inside [CODE] tags, aka "Code:" blocks.

It will make your posts easier to read, & that will get you more, faster, better answers. -- Help us help you.
BTW, You can edit your post(s) to do this retroactively.

Also, please read or re-read the LQ Rules, paying especial note to:
Quote:
  • Do not expect LQ members to do your homework - you will learn much more by doing it yourself.
We, well many of us, will guide you to answers to specific problems; especially is you are up front about the fact that it is homework.


Thank you, & again, welcome.
 
1 members found this post helpful.
Old 04-29-2011, 09:22 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Quote:
Originally Posted by CygwinU View Post
My teacher has a program that will be run to test my program. Therefor i can't use read to make the user input the arguments.
You could if the teacher's program fed the input to your script's stdin but you can't because read is not on the list of allowed commands (neither is [ which is a shell builtin, same as most of the rest of the list) so you can't.
 
Old 04-30-2011, 09:04 PM   #8
CygwinU
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks

Thanks heap for the help guys.

I'm looking for a programming tutor as im so far behind in my Diploma. Can anyone push me in the right direction?
My teacher is incredibly smart as a programmer but he sucks as a teacher.

I need help with my bash and also Java and C++.
I cant find anything legit on programming tutors though i'm willing to pay anyone that has the time and can help me.
Thanks again guys!

p.s. so glad i found this forum.
 
Old 05-01-2011, 05:51 AM   #9
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
The bash guide I referred you to would help. Also the advanced bash scripting guide http://tldp.org/LDP/abs/html/tests.html

If you're looking for a tutor it might help if you provided your location, though the chances of anyone here being close enough to help seem slim. You could look for a local Linux Users Group. You could also try telling your teacher that you are having trouble, they might have contacts who could provide additional tutoring or be able to provide it themselves. If your teacher really does suck as a teacher, (as opposed to it being just you who thinks they do), then you won't be the only one having trouble with courses they teach and you and the other students could get together and raise this with the relevant authority.
 
Old 05-01-2011, 09:30 AM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Midwest USA, Central Illinois
Distribution: SlackwareŽ
Posts: 12,541
Blog Entries: 23

Rep: Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943Reputation: 1943
Hi,

Welcome to LQ!

Quote:
Originally Posted by CygwinU View Post
Thanks heap for the help guys.

I'm looking for a programming tutor as im so far behind in my Diploma. Can anyone push me in the right direction?
My teacher is incredibly smart as a programmer but he sucks as a teacher.

I need help with my bash and also Java and C++.
I cant find anything legit on programming tutors though i'm willing to pay anyone that has the time and can help me.
Thanks again guys!

p.s. so glad i found this forum.
Depending on your level, tutors advertise at Student Centers, Student Affairs or even your instructor may have a select list. You could approach someone in your class that could possibly help.

Linux General Help, Tutorials & Programming General & WEB sections of SlackwareŽ-Links may be of some help.

Just a few links to aid you to gaining some understanding;



1 Linux Documentation Project
2 Rute Tutorial & Exposition
3 Linux Command Guide
4 Bash Beginners Guide
5 Bash Reference Manual
6 Advanced Bash-Scripting Guide
7 Linux Newbie Admin Guide
8 LinuxSelfHelp
9 Utimate Linux Newbie Guide
10 Linux Home Networking
11 Virtualiation- Top 10

The above links and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
1 members found this post helpful.
Old 05-02-2011, 12:09 AM   #11
CygwinU
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Help

Quote:
Originally Posted by arizonagroovejet View Post
The bash guide I referred you to would help. Also the advanced bash scripting guide http://tldp.org/LDP/abs/html/tests.html

If your teacher really does suck as a teacher, (as opposed to it being just you who thinks they do)
Thank you for your help. I sent a partition around the class and i would say 90% signed it agreeing that we need more help or a new teacher.
ofcourse the admin were very protective of their teacher and they brushed it off. Since then, myself and a couple of the other students have resorted to youtube tutorials to get the teacher we need. In saying that, we have passed all that basic tutorials can really teach us and need more.
Thank you for your help once again and i guess its just up to me to figure it all out from here. .

Cheers.
 
Old 05-05-2011, 04:11 PM   #12
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
"I sent a partition around the class..." & not the entire hard drive, quite a feat!

Seriously, I assume you meant "petition". -- You can edit your post to correct the spelling, if you like.

BTW, good luck w/ getting a better teacher. I have 2ce, that I can still remember after 40 years, been stuck in a similar situation; neither ended well.

Last edited by archtoad6; 05-05-2011 at 04:13 PM.
 
  


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
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 09:37 AM
Bash:Printing the line number in bash script suryaemlinux Programming 2 02-05-2011 10:59 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 07:00 AM


All times are GMT -5. The time now is 02:35 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