if statements in shell to compare value


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers if statements in shell to compare value
# 1  
Old 11-15-2011
[Solved] if statements in shell to compare value

Hi,

I am a newbie to unix, I am trying to write a shell script (just a test) but I can't seem to compare the variable value with a string in the if statement.

I get an error when i execute the following script, could you tell me what is wrong and how I can correct it

Note - I am using bash

pasted below is the shell script:


Code:
parameter1=""

if($1 == "-l") then
    $parameter1="-l"
else
    $parameter1="-ltr"
fi

command="ls ${parameter1}"

echo $command

Thanks,
muthu

Last edited by muthuveerappan; 11-15-2011 at 10:07 AM..
# 2  
Old 11-15-2011
Code:
parameter1=""

if ( "$1" == "-l" ) ; then
    parameter1="-l"
else
    parameter1="-ltr"
fi

command="ls ${parameter1}"

echo $command


Last edited by vbe; 11-15-2011 at 10:23 AM..
# 3  
Old 11-15-2011
Since I don't know bash...
Code:
#!/usr/bin/ksh
parameter1=""

if [ "$1" == "-l" ]
then
    parameter1="-l"
else
    parameter1="-ltr"
fi

command="ls $parameter1"

echo $command
#-----
# execution:
n12:/home/vbe $ ./002
ls -ltr
n12:/home/vbe $ ./002 -l 
ls -l

This User Gave Thanks to vbe For This Post:
# 4  
Old 11-15-2011
So possible errors: spaces required... ; then etc...
# 5  
Old 11-15-2011
Thanks a lot !!

Thanks a lot !! works great !! Smilie
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Multiple expect/send statements not working in shell script

Hi I am trying the following in my bash script which logs into my machine and runs a command. Trying to solve this using expect. The first expect statement is hit and it enters the address "10.10.0.10" but when the second expect statement is hit it exits #!/bin/bash expect -c ' spawn... (2 Replies)
Discussion started by: skorada
2 Replies

2. Programming

Shell script - if statements dont work

hi all, i have made a shell script and it runs until it reaches the if statement, doesn't the ! mean only if the command fails it will echo me that message and then exit can anyone please help me what is wrong with my code? many thanks, rob #!/bin/bash echo "is this archive... (10 Replies)
Discussion started by: robertkwild
10 Replies

3. Shell Programming and Scripting

Shell – File Compare

All, I have a set of files in the Source , e-g Afile.DAT Bfile.DAT Cfile.DAT .... AAfile.DAT ...etc I will know for sure the list of file name before I copy the same from the Source area Before I copy the files to Target , I need to make sure that I have received all the files... (5 Replies)
Discussion started by: Shanks
5 Replies

4. Shell Programming and Scripting

Can we use two shebang statements in a single shell script?

Hi, As per my understanding, we can use two shebang statements in a single shell script. Please see below snippet- #!/bin/bash .......## some code A #!/bin/csh .......## some code B exit 0; Here, code A will be executed using bash shell and code B will be executed with c shell. ... (9 Replies)
Discussion started by: tarunmudgal4u
9 Replies

5. Shell Programming and Scripting

Looping through a shell script with sql statements

Hello members, I'm working on the Solaris environment and the DB i'm using is Oracle 10g. Skeleton of what I'm attempting; Write a ksh script to perform the following. I have no idea how to include my sql query within a shell script and loop through the statements. Have therefore given a... (4 Replies)
Discussion started by: novice82
4 Replies

6. Shell Programming and Scripting

Would like to know how to use the IF...THEN...ELSE statements in C shell

Hi I've only been using Unix for a few weeks but am really impressed with its capabilities and ease of use. I am, however, struggling with a shell script at the moment. I would like automated reports on Disk Usage and available free space. I've written a straight forward script that... (0 Replies)
Discussion started by: huskie69
0 Replies

7. UNIX and Linux Applications

How to compare two files using shell script

hi experts please help me to compare two files which are in different directory file1<file will be master file> (/home/rev/mas.txt} ex x1 x2 file2 <will be in different folder> (/home/rev/per/.....) ex x3 x4 the filesinside per folder i need to compare with master file... (1 Reply)
Discussion started by: revenna
1 Replies

8. UNIX for Advanced & Expert Users

how to compare text value in shell

Hi, I need to check that if user is not oracle the the script should exit out . But the below script always exits evenif I am logged as oracle. Plz help me........... USER=`id` echo $USER if ; then echo " You are logged as `id` , it must be oracle " exit 2 fi Regards (2 Replies)
Discussion started by: reply2soumya
2 Replies

9. Shell Programming and Scripting

Running remote shell script containing sql statements

I have a shell script which resides on three SCO machines containing some simple sqlplus statments. I need to run these scripts remotely. Currently, I am trying to use rsh to do so: rsh hostname myscript args The problem is that the arguments to the sqlplus statements in the remote shell... (4 Replies)
Discussion started by: Madbreaks
4 Replies

10. Shell Programming and Scripting

shell script cant recognize if else compare

hi I face the problem the if else statement dint return correct result for me my script as below: #!/bin/ksh sqlplus -s /nolog <<EOF connect databaseuser/password column num new_value num format 9999 set head off select count(*) num from table1; exit num EOF if ; then echo "$?"... (6 Replies)
Discussion started by: jaseloh
6 Replies
Login or Register to Ask a Question