LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash Script Help PLEASE (https://www.linuxquestions.org/questions/linux-newbie-8/bash-script-help-please-877726/)

CygwinU 04-28-2011 08:44 PM

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??

Tinkster 04-28-2011 09:14 PM

Hi, welcome to LQ!

Quote:

Originally Posted by CygwinU (Post 4340085)
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

athrin 04-28-2011 09:29 PM

Quote:

read $num
yeah the $ is wrong.. should't be there

CygwinU 04-29-2011 03:45 AM

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


arizonagroovejet 04-29-2011 04:32 AM

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

archtoad6 04-29-2011 08:14 AM

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.

catkin 04-29-2011 08:22 AM

Quote:

Originally Posted by CygwinU (Post 4340369)
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.

CygwinU 04-30-2011 08:04 PM

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.

arizonagroovejet 05-01-2011 04:51 AM

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.

onebuck 05-01-2011 08:30 AM

Hi,

Welcome to LQ!

Quote:

Originally Posted by CygwinU (Post 4342332)
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!

CygwinU 05-01-2011 11:09 PM

Help
 
Quote:

Originally Posted by arizonagroovejet (Post 4342605)
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.

archtoad6 05-05-2011 03:11 PM

"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.


All times are GMT -5. The time now is 06:40 AM.