Need help with failing simple if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with failing simple if statement
# 1  
Old 05-03-2013
Need help with failing simple if statement

So I'm writing a script that takes in arguments given from the shell. The last argument I'm using as a tag to tell my script, via if statements, to do one thing or another. A sample input would be:

script.scr infile.txt outfile.txt F

The "F" at the end there I expect to be one of two tags "F" or "F.p". In my script, just to give an error if the tags are neither of two things, I have an if statment like:

Code:
if ($#argv = 3)
   set tags = $argv[3]
else set tags = ""
if ( $tags != "F" || $tags != "F.p" ) then
    echo Invalid tag entry.
    exit
endif

The problem is that the echo and exit will execute even if a correct tag is given. I use similar if statements to perform different actions on those files based on the tag given. For example:

Code:
if ($tags == "F") then
   ...
else if ($tags == "F.p") then
   ...
else if ($tags == "") then
   ...
endif

I'm sure that there's just a little thing I'm doing wrong, and any help is appreciated.

Last edited by Scott; 05-03-2013 at 01:47 PM.. Reason: Please use code tags
# 2  
Old 05-03-2013
What are you trying to do?

Using negative and Or conditions can get confusing.
Can you show an example?

This is confusing...
Code:
( $tags != "F" || $tags != "F.p" )

if F.p, then will be true for first condition
if F, then true for second condition

I am thinking you want && and not || for logic.
# 3  
Old 05-03-2013
Haha, you're right I was thinking of &&. Now my only other problem is that second part. Where $tags == "F" and the like don't execute properly. Using -vx will even show "if ( F == F ) then" but it won't execute the code block even though the statement is clearly true.
# 4  
Old 05-03-2013
Something wrong with the structure here, should be: if ( ) then ... else ... endif:
Quote:
Code:
if ($#argv = 3)
   set tags = $argv[3]
else set tags = ""

csh has bad structure control, here the result is nearly unpredictable.
Then, $#argv = 3 is an assignment, you want a comparison.
Then, assignments should be var=value, no spaces!
Code:
if ($#argv == 3) then
   set tags=$argv[3]
else
   set tags=""
endif

Last but not least, you should quote variables references like "$var" whenever possible:
Code:
if ( "$tags" != "F" || "$tags" != "F.p" ) then


Last edited by MadeInGermany; 05-03-2013 at 04:36 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How would I construct a (possibly simple) IF statement?

Hi all, I thought this would be simple, but I've been having a lot of trouble trying to write this IF statement, if I may ask for help pls: In BASH, how would I construct the if statement: Should ONLY be true if USEROPTscript=="yes"]] AND $mode=="INSTALL" /or/ $mode=="CHANGE" ]]... (3 Replies)
Discussion started by: jmccoughlin
3 Replies

2. Shell Programming and Scripting

Simple if statement help needed please

if ]; then echo "successssssssssssssssssss" $filename = "<font color='red'>$i</font>" else echo "failureeeeeeeeeeeeeeeeeeeee" $filename = "$i" fi; I'm just trying to see is this - read a file name and highlight... (2 Replies)
Discussion started by: vmanda
2 Replies

3. Shell Programming and Scripting

If statement failing with error "pc.sh[21]: [yscc02 =yscc02]: not found."

Dear all I wrote a shell script which runs perfect in OEL linux 5.2 but its failing in AIX 5.3 this scripts check if db is up then it wont proceed else proceed with rest steps. any help is appreciated. -------------------------------------------- ORACLE_BASE=/u01/app/oracle... (2 Replies)
Discussion started by: javeedkaleem
2 Replies

4. UNIX for Dummies Questions & Answers

Simple If then statement

Hello, I have never actually created a bash script but thought I would give it a try and got hung up fast.(stupid noob). In the script I have parsed a xml file to get a bunch of file prefixs out. I am then going to check a folder to determine if a file with the correct prefix exists. All the... (5 Replies)
Discussion started by: Dallasbr
5 Replies

5. Shell Programming and Scripting

Help with simple for statement please

Hi guys, I'm typing the following command, but I'm getting a different output than expected. for file in *20* do ls -l $file | wc -l done Instead of listing a total count of the files, it seems to be counting each file and then printing the number of the file - which is 1. How... (7 Replies)
Discussion started by: bbbngowc
7 Replies

6. Shell Programming and Scripting

simple if and then statement

guys, in one of my script, I want to include and a if and then statement which basically does the the following: if a file (constant location and filename) is greater in size than 0 bytes, then it should send out an email. I have tried the following so far with no luck: 1) if # is... (1 Reply)
Discussion started by: admininmaking
1 Replies

7. Shell Programming and Scripting

Simple if statement

Hi there, Im new in the scripting world and ran into trouble yesterday. I want to make a small script that runs in crontab checking folders. If the folder size is greater than 10 (or what I set the variable to) then send a mail with a warning. #!/bin/sh set -x A=8 C=`du -skh... (7 Replies)
Discussion started by: ntenz
7 Replies

8. Shell Programming and Scripting

Using simple if statement in script

Hello, I am trying to create a simple script that will check the disk usage of a drive, and if it is over the limit it will delete the oldest ten files. everything is working except for when i implement the if statement i get the error /usb0: zero divisor. I am pretty sure that the du -k... (1 Reply)
Discussion started by: bpage
1 Replies

9. Shell Programming and Scripting

sh -help with case statement (should be simple)

I'm having an issue running multiple commands in a case statement. If i only run one command it works fine. Am I supposed to use double semi-colons after each statement or do i not need any at all? here is a snippet of the code: case `uname` in "Linux") echo This is linux. cat... (1 Reply)
Discussion started by: kuliksco
1 Replies

10. Shell Programming and Scripting

What's wrong with this simple statement?

I know that this is a ridiculously simple statement, but I am getting an error when I execute it, and I can't figure out what it is. Can anyone point me in the right direction? #!/bin/ksh integer dateMonth=0 integer intZero=0 if then dateMonth = 1 fi echo $dateMonth (7 Replies)
Discussion started by: mharley
7 Replies
Login or Register to Ask a Question