How to reuse values in Shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to reuse values in Shell script
# 1  
Old 09-18-2012
How to reuse values in Shell script

Hi

I am trying to write a shell script and wanted to re-use the value which I have entered already.

Here is the sample code.
Code:
echo "Enter Value : \c" ; read val
echo  "Enter number: $val\c" ; read num

Now I wanted to change the value as showing in 2nd Line or if I will use enter, it should accept as it is.

Thanks in advance.

Last edited by Franklin52; 09-18-2012 at 05:32 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 09-18-2012
This depends heavily on the shell and its version that you are using. In newer versions of bash, for instance, and if the readline packet is installed/used, you can use the -i option to put your variable into the line editing buffer to edit or accept it.
Another way would be to use a third variable to read into, compare to e.g. empty string and, if empty, leave the original value as is, otherwise overwrite it with the new value.
# 3  
Old 09-18-2012
Thanks for your prompt reply.

I am using AIX 6.1 and found that there is no option is working with read (either read -p or read -i).
Is there any other way I could accept the already entered value or enter new value.

Thanks.
# 4  
Old 09-18-2012
Again, this depends on your shell, which you do not mention. Try the second suggestion, sth. like
Code:
read tmp
if [ "$tmp""X" != "X" ]; then val=$tmp; fi

You may be able to use primaries like -n or -z in the test section.

Last edited by RudiC; 09-18-2012 at 10:02 AM..
# 5  
Old 09-18-2012
I am using bash. Also value should not be fix, it will always change.
Code:
echo "Enter Value : \c" ; read val
echo  "Enter number: $val\c" ; read num
if [ "$val""X" != "X" ]; then val=$num; fi

Output is
Code:
Enter Value : 12
Enter number: 1200

I am not able to remove value (12) from 2nd argument (When I am entering Number), so I just enter 00.

---------- Post updated at 01:53 PM ---------- Previous update was at 01:45 PM ----------

Code:
echo "Enter Value : \c" ; read tmp
echo  "Enter number: $val\c" ; read num
if [ "$tmp""X" != "X" ]; then val=$tmp; fi
echo $tmp
echo $num

Output is

Code:
Enter Value : 12
Enter number: 34    [ Here I entered the value as it was not appeard]
12
34

Enter Value : 12
Enter number:   [ Here I just press enter, but the value set to null].
12


Last edited by Franklin52; 09-18-2012 at 05:33 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 09-18-2012
I don't unserstand. In your post #1 you "wanted to re-use the value" which means "don't overwrite/remove". So why do you want to remove it in post #5? This:
Code:
echo "Enter Value : \c" ; read val
echo "Enter number: $val\c" ; read num
if [ "$num""X" != "X" ]; then val=$num; fi
echo val: "$val", num: "$num"

does exactly what you requested: pressing <enter> on the second read retains the value read in first read, and entering a number assigns this number to variable val.
btw, you need to switch on echo's escape interpretation with the -e option.

---------- Post updated at 10:40 AM ---------- Previous update was at 10:37 AM ----------

Quote:
Originally Posted by SushilAnand
---------- Post updated at 01:53 PM ---------- Previous update was at 01:45 PM ----------

Code:
echo "Enter Value : \c" ; read tmp
echo  "Enter number: $val\c" ; read num
if [ "$tmp""X" != "X" ]; then val=$tmp; fi
echo $tmp
echo $num

Output is

Code:
Enter Value : 12
Enter number: 34    [ Here I entered the value as it was not appeard]
12
34

Enter Value : 12
Enter number:   [ Here I just press enter, but the value set to null].
12

Of course the value is null, as it was null before! You have to assign sth. to num that you want to keep. And you need to decide which variable you want to keep: val or num. This is not clear from your text and code.
# 7  
Old 09-18-2012
I want to keep both the value or may be want to use 1st entered value. Thanks.
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. UNIX for Beginners Questions & Answers

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

Need ideas on how to achieve the below. We have a script say "profile.sh" which internally calls another existing script called "name.sh" which prompts for the name and age of a person upon execution. When i run profile.sh how can i populate a pre-defined value from another file and pass that... (1 Reply)
Discussion started by: sankasu
1 Replies

3. Shell Programming and Scripting

Query the table and return values to shell script and search result values from another files.

Hi, I need a shell script, which would search the result values from another files. 1)execute " select column1 from table_name" query on the table. 2)Based on the result, need to be grep from .wft files. could please explain about this.Below is the way i am using. #!/bin/sh... (4 Replies)
Discussion started by: Rami Reddy
4 Replies

4. Shell Programming and Scripting

Shell script to sum every 3 values

I am looking for an easy way to calculate the sum of three values (*-12, *-01, *-02) which are contained after a comma (,). I have found an awk command that will sum every 3rd value, but I am not interested in the values before the comma (,). awk '{s+=$1}NR%3==0{print s;t+=s;s=0}'I am only... (8 Replies)
Discussion started by: ncwxpanther
8 Replies

5. Shell Programming and Scripting

how to reuse a shell script to change env from perl

Hi: I am trying to reuse an existing shell script foo1.csh to set environment variables inside a perl script and its childern processes. Is it possible at all to make those environment variables persistent in the main perl process and its children processes? Do I have to create a new... (4 Replies)
Discussion started by: phil518
4 Replies

6. Shell Programming and Scripting

How to get values from an excel in a shell script

Hi All, Am trying to write a shell script which will get values from an excel and do some calculations. Can any one pls help me out in the commands used to get the values from ms-excel. Thanks!!!:) (2 Replies)
Discussion started by: msri.1900
2 Replies

7. Shell Programming and Scripting

need help on shell script(to pass the values)

only the arguments that are written to the file, my script is (sh /u01app/wkf.sh"$start_no","$name","$Condition","$file_name") like that when ever I run my script I need to write into a new file every time, like wise I have upto10 files with different names.bec my $start_no and $name will... (1 Reply)
Discussion started by: sai123
1 Replies

8. Shell Programming and Scripting

Shell Script: want to insert values in database when update script runs

Hi , I am new to linux and also also to shell scripting. I have one shell script which unpacks .tgz file and install software on machine. When this script runs I want to insert id,filename,description(which will be in readme file),log(which will be in log file) and name of unpacked folder... (1 Reply)
Discussion started by: ring
1 Replies

9. Shell Programming and Scripting

Values of value($$X) in unix shell script

Values of value of x ($$X) in unix shell script program ---------------- #!/bin/ksh t1='/CPI/nodeA/stubs/Test/T1' t2='/CPI/nodeA/stubs/Test/T2' cd $$1--> Parameter may me t1 or t2 ------------------- expecting cd $t1 shold go to the path but it is giving error. (5 Replies)
Discussion started by: ganesh_111
5 Replies

10. Shell Programming and Scripting

Returning Values (shell Script)

I have an application on Informix 4GL, and I am invoking the shell script from the program, but I need to know if the shell script work fine or not, in order to continue the process. I know that we can use $? to get the final status but this is on unix command. How can I return a value from the... (3 Replies)
Discussion started by: jennifer01
3 Replies
Login or Register to Ask a Question