Bash script help..

Bash script help..

Post by Raymond Col » Sun, 21 Apr 2002 00:49:05



HOLA!

I am having a problem with this very basic(to most of you) script any
ideas. all it does is take input from the user and calculate their
age.

The error I get is "integer expression expected" and I am not sure
where to begin? Here is a copy of my script:

---------------------------------

#!/bin/bash
# some comments on the script
echo "Type in your First Name, Month(09) and year(1970) of birth?"
read fn mn yr
if [ "$2" -gt "4" ]
then
mn='expr 12 - $2'
yr='expr 2001 -$3'

else
mn='expr -4 + $2'
yr='expr 2001 - $3'
fi

echo " You, $1, are $yr years with $mn months old."

-------------------------------------------

If I take the dbl quotes off the if stmt I get a unary error. Thank
you for any assistance you may provide.

Ray

 
 
 

Bash script help..

Post by Tom » Sun, 21 Apr 2002 02:36:57


Script works now... Have fun ;-)
----------------------

#!/bin/bash
# some comments on the script
echo "Type in your First Name, Month(09) and year(1970) of birth?"
read name month year

if [ $month > 4 ]
then

mn=`expr 12 - $month`
yr=`expr 2001 - $year`

else
mn=`expr $month - 4`
yr=`expr 2001 - $year`
fi

echo " You, $name, are $yr years and $mn months old."

 
 
 

Bash script help..

Post by Robert_ » Sun, 21 Apr 2002 13:35:00



> Script works now... Have fun ;-)
> ----------------------

> #!/bin/bash
> # some comments on the script
> echo "Type in your First Name, Month(09) and year(1970) of birth?"
> read name month year

> if [ $month > 4 ]
> then

> mn=`expr 12 - $month`
> yr=`expr 2001 - $year`

> else
> mn=`expr $month - 4`
> yr=`expr 2001 - $year`
> fi

> echo " You, $name, are $yr years and $mn months old."

I like this this script.  It knocked 4 months off my age.
Can you do one up for weight ?

; )

Robert_L

 
 
 

Bash script help..

Post by Paul Colquhou » Sun, 21 Apr 2002 15:40:02


|>
|
| Script works now... Have fun ;-)
| ----------------------
|
| #!/bin/bash
| # some comments on the script
| echo "Type in your First Name, Month(09) and year(1970) of birth?"
| read name month year
|
| if [ $month > 4 ]
| then
|
| mn=`expr 12 - $month`
| yr=`expr 2001 - $year`
|
| else
| mn=`expr $month - 4`
| yr=`expr 2001 - $year`
| fi
|
| echo " You, $name, are $yr years and $mn months old."

For definitions of "works" that expire in may this year, and include
producing incorrect answers.

Try something like this instead:

#!/bin/bash
# some comments on the script
echo "Type in your First Name, Month(09) and year(1970) of birth?"
read name month year

# Find out when "now" is
ThisYear=$( date '+%Y' )
ThisMonth=$( date '+%m' )

if [ $month > $ThisMonth ]  # Hasn't had birthday yet this year
then
        # Birthday will be in $month - $ThisMonth months time
        mn=$(( 12 - $month + $ThisMonth ))
        yr=$(( $ThisYear - $year - 1 ))
else
        # Birthday happened $ThisMonth - $month months ago (possibly 0)
        mn=$(( $ThisMonth - $month ))
        yr=$(( $ThisYear - $year ))
fi

if [[ $mn = 0 ]]
then
        echo " You, $name, will be $yr years old this month."
else
        echo " You, $name, are $yr years and $mn months old."
fi

--
Reverend Paul Colquhoun, ULC.    http://andor.dropbear.id.au/~paulcol
     Asking for technical help in newsgroups?  Read this first:
        http://www.tuxedo.org/~esr/faqs/smart-questions.html

 
 
 

Bash script help..

Post by s. keeli » Mon, 13 May 2002 10:44:04



Quote:

> Script works now... Have fun ;-)

> #!/bin/bash
> # some comments on the script
> echo "Type in your First Name, Month(09) and year(1970) of birth?"
> read name month year

> if [ $month > 4 ]
> then

> mn=`expr 12 - $month`
> yr=`expr 2001 - $year`

> else
> mn=`expr $month - 4`
> yr=`expr 2001 - $year`
> fi

> echo " You, $name, are $yr years and $mn months old."

Surely you mean:

#!/bin/bash
#
# some comments on the script
#

echo "Type in your First Name, Month(09) and year(1970) of birth?"
read name month year

if [ $month > 4 ]; then

   mn=`expr 12 - $month`
   yr=`expr 2001 - $year`

else
   mn=`expr $month - 4`
   yr=`expr 2001 - $year`
fi

  :-)

--
Any technology distinguishable from magic is insufficiently advanced.
(*)  TopQuark Software & Services.  Contract programmer, server bum.
- -             Give up Spammers; I use procmail.
How to quote: http://learn.to/quote (Ger.) http://quote.6x.to (Eng.)

 
 
 

Bash script help..

Post by Pascal Bourguigno » Tue, 14 May 2002 09:54:20


1- direct shell questions to comp.unix.shell

2- man bash

3- > is a redirection of output.

   It creates  a file whose name is  written after > and  writes in it
   the output of the command written before.

4- [ is a sym link to test:  ls -l /usr/bin/\[
lrwxrwxrwx    1 root     root            4 Dec 26 01:09 /usr/bin/[ -> test*

5- man test

6- ] is an optional and meaningless argument to test

7- in  bash (sh, etc)  what's expected after  if is a command,  not an
   expression, whose status is used to select the following action.

Therefore:

    month=2 ; if [ $month > 4 ]

is parsed as:

            SYNTAX ERROR
        .                   ]
       / \
      /   \
    'if'  command
            |
            .
           /|\
          / | \
         /  |  \
    'test' arg  redirection-out
            |         |
           '2'       '4'

Anyway,  it seems  that  bash does  not  detect the  syntax error  and
executes that.

    test 2

which returns  a status 0 (= true) for any value of $month
while creating an empty file  named 4 in the current directory, (given
that test does not output anything).

Therefore the then branch of the if is always executed.



> > Script works now... Have fun ;-)

> > #!/bin/bash
> > # some comments on the script
> > echo "Type in your First Name, Month(09) and year(1970) of birth?"
> > read name month year

> > if [ $month > 4 ]
> > then

> > mn=`expr 12 - $month`
> > yr=`expr 2001 - $year`

> > else
> > mn=`expr $month - 4`
> > yr=`expr 2001 - $year`
> > fi

> > echo " You, $name, are $yr years and $mn months old."

> Surely you mean:

> #!/bin/bash
> #
> # some comments on the script
> #

> echo "Type in your First Name, Month(09) and year(1970) of birth?"
> read name month year

> if [ $month > 4 ]; then

>    mn=`expr 12 - $month`
>    yr=`expr 2001 - $year`

> else
>    mn=`expr $month - 4`
>    yr=`expr 2001 - $year`
> fi

>   :-)

> --
> Any technology distinguishable from magic is insufficiently advanced.
> (*)  TopQuark Software & Services.  Contract programmer, server bum.
> - -             Give up Spammers; I use procmail.
> How to quote: http://learn.to/quote (Ger.) http://quote.6x.to (Eng.)

--
__Pascal_Bourguignon__            (o_ Software patents are endangering
()  ASCII ribbon against html-    //\ the computer industry all around
/\ email & Microsoft attachments. V_/ the world http://lpf.ai.mit.edu/
1962:DO5K=1.3  2001:my($f)=`fortune`;  http://petition.eurolinux.org/

You're a web designer?   Please read http://www.useit.com/alertbox/ !

-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/IT d? s++:++(+++)>++ a C+++  UB+++L++++$S+X++++>$ P- L+++ E++ W++
N++ o-- K- w------ O- M++$ V PS+E++ Y++ PGP++ t+ 5? X+ R !tv b++(+)
DI+++ D++ G++ e+++ h+(++) r? y---? UF++++
------END GEEK CODE BLOCK------

 
 
 

Bash script help..

Post by Dave Bro » Tue, 14 May 2002 15:03:21




> ...
> 4- [ is a sym link to test:  ls -l /usr/bin/\[
> lrwxrwxrwx    1 root     root            4 Dec 26 01:09 /usr/bin/[ -> test*

There is a link called "[" to the executable "/usr/bin/test".  This
executable was created for the original Bourne shell.  However,
when using a test in bash, you are using the "shell builtin", (unless, of
course, you specify the path name).

Quote:> 7- in bash (sh, etc)  what's expected after if is a command, not an
> expression, whose status is used to select the following action.
> Therefore:
>  month=2 ; if [ $month > 4 ]

Needless to say, if you're doing a "numerical" test, the expression should
read:  [ "$month" -gt 4 ]

bash also supports string tests:
       [ "$mo" = "Jan" ]
and    [ "$mo" != "Oct" ]

But with the "[[" instead of "[", you can check strings for "earlier"
in the collating sequence, or "later":
       [[ "abc" <= "def" ]]   (true)
well this would "less (earlier) than or equal to"; and:
       [[ "xyz" > "abc" ]]  (true)

(Notice, no redirection here; the "[[" protects the ">" just like
quote marks, so it's properly interpreted as a comparison operator.)

--
Dave Brown  Austin, TX

 
 
 

1. bash script help

I have no bash script skills at all

I want to convert collection of mp3 files
to wave or cdr
Using a script ripped off from the cdwrite howto ( below )
the result is a wave file with two extensions "file.mp3.wav".
Obviously I want only ".wav".

The "correct" script would be appreciated.

#!/bin/bash

for I in *.mp3

do

        mpg321 --w - "$I"  > "$I".wav

done

--
faeychild

2. Solaris 9 ipqos issues?

3. NIS and Sendmail

4. simple bash script help

5. Q: turning off the hard drives

6. Linux BASH Script HELP!!!!!

7. MkLinux problem on G3

8. Passing option to bash script help needed

9. Simple bash script help?

10. re bash script help

11. BASH script help