need to pass value while running the script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need to pass value while running the script
# 8  
Old 07-19-2012
Quote:
Originally Posted by balajesuri
Code:
#!/bin/ksh

echo "The value of the first argument to the script: $1"
password="asr"

if  [ $1 == "$password" ]
then
    echo "You have access!"
else
    echo "ACCESS DENIED!"
fi

read is used to take input from user. That's why you saw what you quoted.
You did not do this changes as suggested.
Take away the read, the value is already in $1
And I think you should have quotes surrounding $1 "[": if [ "$1" = "$password" ]
I don't know about ksh, but in sh you should use single "=".
# 9  
Old 07-20-2012
Java

here come another question.


I was reading about if else in a book.
Below script was given a example.

One weird thing which I noticed was , this will only read that file which is created through this script.
Even if you try to make this script read another file(dat file)if will only show the output of the file
Which is created by this.

I created a file khare using this script and pass age 27 in the file.


Code:
#!/bin/sh
   
  # Prompt for a user name...
  echo "Please enter your name:"
  read USERNAME
   
  # Check for the file.
  if [ -s ${USERNAME}_DAT ]; then
          # Read the age from the file.
          AGE=`cat ${USERNAME}_DAT`
          echo "You are $AGE years old!"
  else
          # Ask the user for his/her age 
          echo "How old are you?"
          read AGE
   
                 if [ "$AGE" -le 2 ]; then
                                 echo "You are too young!"
                 else
                                 if [ "$AGE" -ge 100 ]; then
                                                echo "You are too old!"
                                 else
                                 # Write the age to a new file.
                                 echo $AGE > ${USERNAME}_DAT
                 fi
          fi
  fi

now when I run the script , passing the file khare(created through this script ) it show output as below.
Code:
$
  Please enter your name:
  khare
  You are 27 years old!
  You are 27 years old!
  $

Now I created a file by vi command , file name as D_DAT and content inside this is FW
Now when I ran the script
It ask for file mane which I gave as a D_DAT . now what I see now is instead of displaying “FW” it is again asking the age .
Now I pass 45 for the age.
After this I noticed that script has created the a new file as D_DAT_DAT.


I am fail to understand why this script is not reading the file which is already present in the system.

Last edited by scriptor; 07-20-2012 at 08:30 AM.. Reason: miss some info
# 10  
Old 07-20-2012
this is working as expected... check again....

Code:
]$ sh test_tmp.sh
Please enter your name:
pd
How old are you?
25
]$ sh test_tmp.sh
Please enter your name:
pd
You are 25 years old!

# 11  
Old 07-20-2012
Java

can you please create the the file manually and the run the script.
and see if you are still getting the same result ?
# 12  
Old 07-20-2012
Code:
]$ echo "36" > pd_DAT
]$ sh test_tmp.sh
Please enter your name:
pd
You are 36 years old!
]$ echo "39" > pp_DAT
]$ sh test_tmp.sh
Please enter your name:
pp
You are 39 years old!

And i have tried with a file which i have created in my windows machine and then copied to server.. still the result is same..
Code:
]$ sh test_tmp.sh
Please enter your name:
kk
You are 99 years old!

# 13  
Old 07-20-2012
Java

not working in my case
i created the D_DAT file manually but when i tried call it through it simply creating another file and asking of the age again.
see o/p as below
Code:
3 Jul 20 16:42 D_DAT
3 Jul 20 16:43 D_DAT_DAT

# 14  
Old 07-20-2012
Quote:
Originally Posted by scriptor
/.../
...
now when I run the script , passing the file khare(created through this script ) it show output as below.
Code:
$
  Please enter your name:
  khare
  You are 27 years old!
  You are 27 years old!
  $

The error here is that you probably were using your script with that file as input your_script < khare_DAT or?
If you do that it will use the lines from the file instead of asking for input. Or I'm not shure exactly what you tested, but perhaps you mix up the input and the name of the file, if you want the file "khare_DAT" to be used you should only enter "khare", and not use the file as input to the script.
Quote:
Now I created a file by vi command , file name as D_DAT and content inside this is FW
Now when I ran the script
It ask for file mane which I gave as a D_DAT . now what I see now is instead of displaying “FW” it is again asking the age .
Now I pass 45 for the age.
After this I noticed that script has created the a new file as D_DAT_DAT.
Here you should have used only "D" as input, if you use "D_DAT" it will create a file named "D_DAT_DAT" because of
Code:
echo $AGE > ${USERNAME}_DAT


Last edited by 244an; 07-20-2012 at 11:17 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass values to a script called from within another script in shell?

Ceiling Light - The Forgotten Element One of the highest details concerning using an LED ceiling panel essentially offer a fantastic dance floor which definitely makes the customers dance right away.They are a quite low cost method of something like a lighting solution, simple collection up,... (1 Reply)
Discussion started by: harveyclayton
1 Replies

2. Shell Programming and Scripting

Shell script to pass the config file lines as variable on the respective called function on a script

I want to make a config file which contain all the paths. i want to read the config file line by line and pass as an argument on my below function. Replace all the path with reading config path line by line and pass in respective functions. how can i achieve that? Kindly guide. ... (6 Replies)
Discussion started by: sadique.manzar
6 Replies

3. Shell Programming and Scripting

Need to pass arguments while running a Shell Script

Shell Script Gurus, I am writing a shell script which needs User ID's to pass as an Arguments in command line while executing. Can this be doable? I've never done this, If you give a sample script that would be helpful, Thanks. (1 Reply)
Discussion started by: shekar777
1 Replies

4. Shell Programming and Scripting

Pass script with parameter in korn shell script

I have written a script which will take input parameter as another script. However, if the script passed as input parameter has parameters then this script doesn't work. I have a script b.ksh which has 1 and 2 as parameters I have a script c.ksh which has 3,4 and 5 as parameters vi a.ksh... (1 Reply)
Discussion started by: Vee
1 Replies

5. Shell Programming and Scripting

How to pass Oracle sql script as argument to UNIX shell script?

Hi all, $ echo $SHELL /bin/bash Requirement - How to pass oracle sql script as argument to unix shell script? $ ./output.sh users.sql Below are the shell scripts and the oracle sql file in the same folder. Shell Script $ cat output.sh #!/bin/bash .... (7 Replies)
Discussion started by: a1_win
7 Replies

6. Post Here to Contact Site Administrators and Moderators

Unable to pass shell script parameter value to awk command in side the same script

Variable I have in my shell script diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk -F'~' ''$2 == "$id"' {print $0}' > $new I could see value of $id is not passing to the awk... (0 Replies)
Discussion started by: Ashunayak
0 Replies

7. Shell Programming and Scripting

Pass parameters to a function and running functions in parallel

Hi , I have a script which is using a text file as I/P. I want a code where it reads n lines from this file and pass the parameters to a function and now this script should run in such a way where a function can be called in parallel with different parameters. Please find below my script, it... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

8. Shell Programming and Scripting

How we can pass the argument when calling shell script from perl script

Can someone let me know how could I achieve this In one of per script I am calling the shell script but I need to so one thing that is one shell script call I need to pass pne argument.In below code I am calling my ftp script but here I want to pass one argument so how could I do this (e.g:... (5 Replies)
Discussion started by: anuragpgtgerman
5 Replies

9. Shell Programming and Scripting

pass null value to sql script from korn shell script

There are 4 parameters that I have to pass from korn shell to sql script. 1) I have to check if $1 , $2 , $3 and $4 are null values or not . How can I do that ? 2) Once its determined that these values are null (in the sense they are empty) how can I pass null values to sql script... (11 Replies)
Discussion started by: megha2525
11 Replies

10. UNIX for Advanced & Expert Users

Pass parameter to the main script from wrapper script

Hi, I am writing a wrapper script(wrap_script.sh) to one of the main scripts (main_script.sh) The main script is executed as following: ./main_script.sh <LIST> <STARTDATE> <ENDDATE> looks for a parameter which is a LIST(consists of different list names that need to be processed), START/END... (0 Replies)
Discussion started by: stunnerz_84
0 Replies
Login or Register to Ask a Question