having problems with IF statements


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting having problems with IF statements
# 1  
Old 02-18-2009
having problems with IF statements

below is a sample of the shell script im trying to get working. if the script is run with no data it should display the usage. if data1 is given it should grep with todays date and if data1 and a date are giving it should grep using the date given. please let me know what im doing wrong ive tried everything. Thanks.

Code:
#!/bin/sh
autodate=`date "+%Y%m%d"`
data1=$1
setdate=$2
if [ $data1 = $NULL ]; then
    echo "Usage - enter the data1 then the date in YYYYMMDD format"
    echo "Usage - You can also enter just the data1 to use autodates Date"
    echo "Example: sh $0 data $autodate"
    exit
fi
if [ $setdate = $NULL ]; then
    echo "Searching for data1: $data1 on date: $autodate"
    cd /dir/
    grep $data1 $autodate*.log
    echo Done searching
    cd /home/
    exit
fi
if [ $setdate != $NULL ]; then
    echo "Searching for data1: $data1 on date: $setdate"
    cd /dir/
    grep $data1 $setdate*.log
    echo Done searching
    cd /home/
    exit
fi

ive also tried
Code:
if [ ! -n $data1 ]; then

and
Code:
if [ -z $data1 ]; then


Last edited by jrelax; 02-18-2009 at 05:04 PM..
# 2  
Old 02-18-2009
guys never mind i figured out what i was doing wrong. i got it working with ! -n "$data1". i was missing the quotes "" around my variable.
# 3  
Old 02-18-2009
forget $NULL -- that's not doing what you want....

first, why not just this:

Code:
if [ $# -ne 2 ]; then
  echo Usage: $0 data1 setdate
  echo where setdate is in mm/dd/yyyy format
  exit 1
fi

# 4  
Old 02-18-2009
Quote:
Originally Posted by quirkasaurus
forget $NULL -- that's not doing what you want....

first, why not just this:

Code:
if [ $# -ne 2 ]; then
  echo Usage: $0 data1 setdate
  echo where setdate is in mm/dd/yyyy format
  exit 1
fi


the reason i didn't go with that is because that would exit if 2 values weren't given. i want the script to work with 1 value as the second can be calculated from the date
# 5  
Old 02-18-2009
Quote:
Originally Posted by jrelax
the reason i didn't go with that is because that would exit if 2 values weren't given. i want the script to work with 1 value as the second can be calculated from the date
Ok. Well, I was really talking about using that technique,
not necessarily that exact code...

ie:

Code:
 
if [ $# -eq 2 ]; then
  setdate=$2
  data1=$1
elif [ $# -eq 1 ]; then
  data1=$1
  ... derive setdate...
else
  echo usage: data1 '[setdate]'
  exit 1
fi

IMHO -- it's a little more standardized approach than
checking for lengths of various positional parameters.
# 6  
Old 02-18-2009
Quote:
Originally Posted by quirkasaurus
Ok. Well, I was really talking about using that technique,
not necessarily that exact code...

ie:

Code:
 
if [ $# -eq 2 ]; then
  setdate=$2
  data1=$1
elif [ $# -eq 1 ]; then
  data1=$1
  ... derive setdate...
else
  echo usage: data1 '[setdate]'
  exit 1
fi

IMHO -- it's a little more standardized approach than
checking for lengths of various positional parameters.

thanks man. i rewrote my script based on your model. I'm new to this and had a hard time with the expressions and the proper way to use them i also took out the variables and just used $1 and $2 everywhere
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Execution Problems with if statements

Hi all, I habe a file called test.log, which contain following data : 0.0 0.1 0.1 0.1 0.1 0.2 0.3 0.3 0.4 0.4 0.6 8.7 8.8 17.2 I want to show the data which gater than 9.0 But my script not working. (4 Replies)
Discussion started by: mnmonu
4 Replies

2. Shell Programming and Scripting

Too many if statements..

Hello. I am new here and new to scripting. I used to have a very basic script that worked for simple backup/restore of files. I have expanded it and well... I have ended up with a complete mess. It still backs up and restores but there is so many issues that stem from the many if statements I... (3 Replies)
Discussion started by: gameinn
3 Replies

3. UNIX for Dummies Questions & Answers

if and then statements

I came across a bash script that outputs the forecast for the day and the max temperature but at the end of the day the max temperature disappears ($6) and I am left with "°C" after the forecast. Here is the script: #! /bin/bash curl -s --connect-timeout 30... (7 Replies)
Discussion started by: _light_
7 Replies

4. Shell Programming and Scripting

Using While and If statements

Hi guys, Two problems I need solving please. I created a script where the user types in 7 numbers as standard input and each one is then stored in an array. Now I need to perform the following calculations on those numbers: 1) Use a while loop to determine the largest number in the range. ... (2 Replies)
Discussion started by: jjb1989
2 Replies

5. Shell Programming and Scripting

If statements....

Good morning all! I want to know if Im interpreting this if statement below right. if(((1) || (0)) && (1)){ do stuff; } This is saying: if true piped into false, then true, then do stuff. Right? What does the && stand for? thanks in advance! ben (3 Replies)
Discussion started by: bigben1220
3 Replies

6. UNIX for Dummies Questions & Answers

Help with For Statements

Hi, I am trying to write a for statement that will allow for the ps, who, finger, and date commands to run. Can anyone help? I use Putty. (22 Replies)
Discussion started by: lexydoll87
22 Replies

7. Shell Programming and Scripting

HELP!! if statements

I am kind of new in Unix and i have to make a menu. I want to put an if statement in the menu. you should enter the filename and it goes to that file. How do i do this? (1 Reply)
Discussion started by: trob
1 Replies

8. UNIX for Advanced & Expert Users

if statements

This is for a program I have to do to calculate the day of the week. I need to write an if statement that will do the following: if day is 29 and year is odd, don't calculate dayif ( day == 29 && year == ??? )I know how to do it for the day but I don't know how to do it for the year. (4 Replies)
Discussion started by: pwanda
4 Replies

9. Shell Programming and Scripting

Major Awk problems (Searching, If statements, transposing etc.)

Please bare with me as task is very detailed and I'm extremely new to Awk/sed. Keep in mind I'm running windows so I'm using a dos prompt The attachment is a server report that I'm trying to manipulate with little success. For each server, I need to take the most recent information about them... (2 Replies)
Discussion started by: Blivo
2 Replies

10. Shell Programming and Scripting

or statements?

how do i do an or in an if-then statement? i tried: if ; then bleh fi how???? (1 Reply)
Discussion started by: Blip
1 Replies
Login or Register to Ask a Question