How to set value to variable in UNIX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to set value to variable in UNIX
# 1  
Old 07-14-2009
How to set value to variable in UNIX

Hi,

I am new to UNIX. I wonder how to set value to variable in UNIX..

my code:
if [ -f /home/batch/sample.txt ]; then EXIST=$a ; else EXIST=$b ; fi'
echo $EXIST


the value of EXIST is empty...

Thanks Smilie
# 2  
Old 07-14-2009
Initially assign some values to variable "a" and "b" as
a=10 <sample_data>
b=11 <sample_data> Then check
# 3  
Old 07-14-2009
Is $a and/or $b defined ?
# 4  
Old 07-14-2009
if I use this code, it works fine.
Code:
ssh batch@192.168.3.222 'if [ -f /home/batch/sample.txt ]; then echo "exist" ; else echo "not exist" ; fi'

it will print out "Exist"



But if I use this code, it display "0".
Code:
a=0
ssh batch@192.168.3.222 'if [ -f /home/batch/sample.txt ]; then a=1 ; else a=2 ; fi'
echo $a

It seems that the value is not assigned to the variable inside the if-else statement.
# 5  
Old 07-14-2009
yes, because you ran it to a different machine
Code:
a=`ssh batch@192.168.3.222 'if [ -f /home/batch/sample.txt ]; then echo 1 ; else echo 2 ; fi'`
echo $a

# 6  
Old 07-14-2009
Quote:
Originally Posted by ryandegreat25
yes, because you ran it to a different machine
Code:
a=`ssh batch@192.168.3.222 'if [ -f /home/batch/sample.txt ]; then echo 1 ; else echo 2 ; fi'`
echo $a

Hi ryandegreat25,
Thanks.. it works now... Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Linux

Use read to set a variable.

Hi all, I used to set variable by read from keyboard read -p 'Input new value for variable :' var Now I want to pipe from ls and set to var a.txt b.txt c.txt ls | grep a.txt | read var why this cannot set the $var. What is the different between them....:wall: (4 Replies)
Discussion started by: mainsun
4 Replies

2. Shell Programming and Scripting

do you have a better way to set this variable?

greetings, i have a variable $input that i want to use to set $output. $input is /dir/filename.mph and $input is passed to my script that i manipulate it as follows: input=`basename $input`i want the $output to be filename_solved.mph, basically stuffing "_solved" in the filename. here's how i... (2 Replies)
Discussion started by: crimso
2 Replies

3. Shell Programming and Scripting

How to know who and where a variable is set ?

hi, i'm not a root user and i want to know which user and in which file is loaded a variable seen in the "env" display ? I will use this variable but i want to be sure that it will be a permanent variable ! i don't see it in my files (.profile , kshrc...) and neither in /etc/profile. ... (3 Replies)
Discussion started by: Nicol
3 Replies

4. HP-UX

What is the use of command set -- and set - variable?

Hi, I am using hp unix i want to know the use of the following commands set -- set - variable thanks (4 Replies)
Discussion started by: gomathi
4 Replies

5. Shell Programming and Scripting

set variable in while loop?

Dear All, Can anyone advise why this script isn't run as expected? =========================== status=0 cat /etc/passwd | while read line; do status=1 done echo $status =========================== it always return 0 , but not 1. why? anything wrong? Thanks. (1 Reply)
Discussion started by: tiger2000
1 Replies

6. Shell Programming and Scripting

Set a variable that changes every time?

Do not know how to do this, any help would be appreciated: I have a file that comes in called xxxx.txt. I have a script that does some messing around with the file. The file needs to go out as PAB108XXXX.csv The four x's above will be a number that changes everytime the script is ran e.g.... (1 Reply)
Discussion started by: Pablo_beezo
1 Replies

7. Solaris

set environment variable?

I am working with solaris 9 sunBlade150 Box. I Installed a program, need to set the environment variable so that when the executable is entered,it finds the path to the executable. The documentation for the software says: Set the appropriate environment variable: Connect to server failed;... (8 Replies)
Discussion started by: smartgupta
8 Replies

8. Shell Programming and Scripting

set variable with another variable? c shell

okay, this shouldn't be difficult but I can't figure it out. How can I set a variable with another variable. I have the following: foreach pe ($dir $sp) set tpe = `echo $pe | grep M` if ($tpe == M) then set ${$pe} = M <--- This doesn't work else endif end In this case what... (2 Replies)
Discussion started by: wxornot
2 Replies

9. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

10. UNIX for Dummies Questions & Answers

Where to set the LANG variable

When I login to my AIX server, the LANG variable is automatically being set to En_US. This is causing locale warning messages when I run emacs or perl. The reason seems to be that En_US is not a valid locale (seen by running locale -a). I can change the variable from the command line (export... (3 Replies)
Discussion started by: wvdeijk
3 Replies
Login or Register to Ask a Question