$1 argument not being recognized


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting $1 argument not being recognized
# 1  
Old 07-28-2011
$1 argument not being recognized

I am working on AIX 6.1 issuing #!/bin/sh at the beginning of the script.

I don't understand why $1 is not working as expected. I expect it to hold the value for the first argument coming from the command line consistently - and it is not.

At the beginning of the script starting I do this:
Code:
echo "Number of arguments entered: $# "
echo $1
chk_date

When I echo $1 here it shows the output when the script is run:
Code:
Number of arguments entered: 1
07/28/2011

But when I do the same thing within the function chk_date() it won’t display it:
Code:
chk_date()
{

if [ "$1" != $TODAYDT ]
then
     echo "YOU ENTERED"
   echo $1     
     echo "TODAY IS ---> $TODAYDT"
     echo "YOU HAVE ENTERED A DATE THAT DOES NOT MATCH TODAY'S DATE"
     echo "DO YOU WANT TO USE TODAY'S DATE INSTEAD?  Y|y or N|n"
     read ANSWER

Script is excuted like this:
Code:
[doruatt01pu]/RRPS/devel/ora/mig: migmstr.sh_fc 07/28/2011

Full output
Code:
Number of arguments entered: 1
07/28/2011
YOU ENTERED

TODAY IS ---> 07/28/2011
YOU HAVE ENTERED A DATE THAT DOES NOT MATCH TODAY'S DATE
DO YOU WANT TO USE TODAY'S DATE INSTEAD?  Y|y or N|n


The first problem is that it is validating true as a non-match with the variable $TODAYDT which has the value of 07/28/2011 as shown in green.
See … below the YOU ENTERED in the output? It is blank – even though I’m doing the same echo command that displayed it as 07/28/2011 at the beginning of the script… I’m really stumped on this… please help. Thank you.

Last edited by radoulov; 07-28-2011 at 11:46 AM.. Reason: Code tags!
# 2  
Old 07-28-2011
Quote:
Originally Posted by Feliz
...
At the beginning of the script starting I do this:
echo "Number of arguments entered: $# "
echo $1
chk_date

When I echo $1 here it shows the output when the script is run:
Number of arguments entered: 1
07/28/2011

But when I do the same thing within the function chk_date() it won't display it:
chk_date()
...
When you are defining a routine the global $1 goes out of scope and $i points to the first arg of the routine.

try calling it as follows
Code:
echo "Number of arguments entered: $# "
echo $1
chk_date($1)

# 3  
Old 07-28-2011
Additional info...

It has something definitely to do with the echo $1 being inside the function chk_date(). I modified the script so that it would not have the function chk_date() and it works as expected:

Code:
[doruatt01pu]/RRPS/devel/ora/mig: migmstr.sh_fc 07/28/11
Number of arguments entered: 1
06/28/2011
YOU ENTERED
06/28/2011
TODAY IS ---> 07/28/2011
YOU HAVE ENTERED A DATE THAT DOES NOT MATCH TODAY'S DATE
DO YOU WANT TO USE TODAY'S DATE INSTEAD?  Y|y or N|n

So, I’d like to know why a custom function changes the functionality of the $1 when used within that function. Thanks!

Last edited by radoulov; 07-28-2011 at 12:05 PM.. Reason: Code tags.
# 4  
Old 07-28-2011
I suppose you need:

Code:
chk_date "$1"

instead of:

Code:
chk_date

# 5  
Old 07-28-2011
Thanks, Skrynesaver, for your quick reply - I didn't see your post before I sent the Additional info post...

I tried to pass the $1 into the function and it gives me a syntax error:
+30
+31 chk_date($1)


Code:
[doruatt01pu]/RRPS/devel/ora/mig: migmstr.sh_fc 06/28/2011
migmstr.sh_fc[31]: syntax error at line 31 : `(' unexpected

---------- Post updated at 11:06 AM ---------- Previous update was at 11:03 AM ----------

Thanks, Radoulov -- that DOES work!

Learned something new -- thank you both!

Last edited by radoulov; 07-28-2011 at 12:05 PM.. Reason: Code tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Error:--test: argument expected--Even though i give an argument.

Hi All, I am running the script VBoxManage list vms |sed 's/"//g' | cut -d " " -f1 > har1out.mytxt result=`cat har1out.mytxt | grep $1' echo $result echo $1 { if then echo pass else echo fail fi (2 Replies)
Discussion started by: harsha85
2 Replies

2. Shell Programming and Scripting

Alias not recognized within script

What would cause a script to work under one user account and not another? Here's an example of what I'm referring to. Here's a simple script. Let's put it in a file called “thescript”. #! /bin/bash alias a='echo hello world' a Here are the results when this script is executed logged in... (3 Replies)
Discussion started by: sszd
3 Replies

3. Shell Programming and Scripting

Second parameter is not recognized

Hey I have a problem with my script. I writing a script which can depend whether you want to make a file or a directory. I only give the code where you can find the problem. My loop doesn't stop and I don't know why and when i echo my second parameter , he echoes nothing :s Thnx ... (1 Reply)
Discussion started by: Eclecticaa
1 Replies

4. Shell Programming and Scripting

Cannot compare argument in if statement in csh/grep command if argument starts with “-“

If ($argv == “-debug”) then Echo “in loop” Endif But this is not working. If I modify this code and remove “-“, then it works. Similarly I am getting problem using grep command also Grep “-debug” Filename Can someone please help me on how to resolve these... (1 Reply)
Discussion started by: sarbjit
1 Replies

5. Shell Programming and Scripting

File not recognized when passed as argument

I have the below script in file read_file.ksh if ] || ] then echo "Required one input file" echo "Enter a file to get char count:" read $FILE_NAME if ] then echo "valid file" else echo "Not a valid file." fi When run as read_file.ksh detail.csv or... (9 Replies)
Discussion started by: michaelrozar17
9 Replies

6. Solaris

New volume set not recognized

Good evening, I am fairly (re)new to Solaris 10. In fact I was even quite good at it, but that was about 10 years ago, so now I am back to beginner status :) I installed a new machine with an internal 16port RAID attached to an Areca 1260 controller. Also attached is an external SCSI-Raid. ... (7 Replies)
Discussion started by: PatrickBaer
7 Replies

7. Shell Programming and Scripting

get positive number n as argument script must calculate the factorial of its argument

Can someone please help me with this SHELL script? I need to create a script that gets a positive number n as an argument. The script must calculate the factorial of its argument. In other words, it must calculate n!=1x2x3x...xn. Note that 0!=1. Here is a start but I have no clue how to... (3 Replies)
Discussion started by: I-1
3 Replies

8. UNIX for Dummies Questions & Answers

How to find the last argument in a argument line?

How to find the last argument in a argument line? (4 Replies)
Discussion started by: nehagupta2008
4 Replies

9. Forum Support Area for Unregistered Users & Account Problems

I am not being recognized

Hi Admin, My user id is "chanakyahere", i forgot my password. When i give my mail id "chanakyahere@gmail.com" it is not being accepting by webmaster. But to the same mail id i got reply to one of my questions. The same mail id i gave while registering in this site. This i can confirm as... (0 Replies)
Discussion started by: Help
0 Replies

10. Shell Programming and Scripting

Argument not recognized as integer

I need to accept a number of arguments at command line and print it in reverse order i use eval `echo x=$1` to capture the argument #! /bin/sh counter=0 while do eval `echo x=$1` arg$counter=$x counter=`expr $counter + 1` shift done but the error keeps... (1 Reply)
Discussion started by: scmay
1 Replies
Login or Register to Ask a Question