If Then ElseIf Script - Confusion Around Expression's Syntax


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers If Then ElseIf Script - Confusion Around Expression's Syntax
# 1  
Data If Then ElseIf Script - Confusion Around Expression's Syntax

Hello,

I am relatively new to UNIX scripting and am learning a lot. I have already tried several searches on this website and have tried various syntax options suggested to no avail. I am obviously not writing the script correctly. I really do appreciate any and all the help.

Below is an outline of what I am looking to accomplish. The text in blue highlights the sections I am struggling with. Smilie

GOALS:
IF
The file, /test/me/test.csv is less than 295 bytes
THEN
Do commands here (already have this part written successfully)

ELIF
The file, /test/me/test.csv is >= 295 bytes AND if the FIRST row in that file contains the header string that begins with, ie. Tick_Num,Line_No,*
THEN
Do commands here (already have this part written successfully)

ELIF
The file /test/me/test.csv = 0 bytes
THEN
Do commands here (already have this part written successfully)
FI

Again, thank you very much.

D

Last edited by dqrgk0; 09-13-2011 at 12:07 PM.. Reason: code tags, see PM
# 2  
In your elif, how can a file have no size and still have a header row? Perhaps there's a typo?
# 3  
The elif-part doesn't make sense, because when the file has size 0 it can't have any content to be found by grep, as !s means the opposite, ie. <=0.

If you want to issue a command inside a test, you coud try like this:
Code:
if [[ -s /etc/passwd && $(head -1 /etc/passwd| grep -q root > /dev/null 2>&1; echo $?) == 0 ]]; then
    echo yo
else
    echo no
fi

... or as a separate sub if/then/fi below the check if the file exists and has a size >0.

To grep for more than one pattern as a logic "OR", you can use this:
Code:
grep -E "^Tick_Num|^Line_No" infile

The ^ tells it that it is the start of a line, ie. 1st character to follow and the pipe | separates the patterns.
A * would be writtens as .* in Regular Expressions and this would make no sense, as this could be everything and sowith always be true.

Another hint is to declare a variable, that contains the file name, since it is awful to type it over and over and when you want to change it, you change it at just 1 place.

Edit:
If "Tick_Num,Line_No,*" is a complete pattern, forget the lines above about grep -E

Last edited by zaxxon; 09-13-2011 at 11:50 AM.. Reason: added note
# 4  
First of all, it would be helpful if you posted the error message too.
Second, you can get additional debugging output if you insert the line set -x near the start of your script.
Third, there's an unbalanced quote in your if:
Code:
if [ -s /test/me/test.csv && grep Tick_Num,Line_No,*' /test/me/test.csv ]

# 5  
Here's one way, that while may not be the slickest, breaks down the issue into logical parts that you are testing for and may be useful for understanding:
Code:
#!/bin/ksh

FILE=test.csv  # Path to file to test.

if [[ -s $FILE ]]; then  # If it exists and has size...
   ##  See if the 1st line starts with the header string
   ##  Throw away the output and test for the return code
   head -1 $FILE | grep '^Tick_Num,Line_No' >/dev/null 2>&1
   if (( $? == 0 )); then
     print "$0: Found a header in the first row"
   else
     print "$0: $FILE does not have a header row"
     exit 1
   fi
else
  print "$0: $FILE does not exist or has no size"
  exit 2
fi

exit 0


Last edited by gary_w; 09-13-2011 at 11:49 AM.. Reason: I see your requirements changed from the original post, so this example is no longer valid.
# 6  
If requirements changed, then post this as a reply please. Don't expect people to check your original post all the time to see if something changed, thanks.
# 7  
Sorry folks - I did have a bunch of typos in my original posting. I'm really sorry about that. I will try the suggestions provided so far and let y'all know!

Thanks,
D

---------- Post updated at 10:19 AM ---------- Previous update was at 09:52 AM ----------

I think since my requirements drastically changed, a lot of the suggestions may not apply...If anyone can forgive my ignorance and update or provide other solutions, I would appreciate it.

Again, apologies.

D
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #793
Difficulty: Easy
The base-2 numeral system is a positional notation with a radix of 3.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Confusion with Reg expression

I want to make a REG Expression to validate the directory. my dirsample is below: /abc/abc/abc abc/abc/abc abc/abc/abc/ /abc/a bc/abc /a bc/abc/abc /abc/abc/a bc / abc/abc/abc /abc/ abc/abc /abc/.abc /.abc/abc / // /abc /.abc And my code is below: grep -E '^\/(+\/?)+$' dirsample (4 Replies)
Discussion started by: franksunnn
4 Replies

2. Shell Programming and Scripting

Help with awk script (syntax error in regular expression)

I've found this script which seems very promising to solve my issue: To search and replace many different database passwords in many different (.php, .pl, .cgi, etc.) files across my filesystem. The passwords may or may not be contained within quotes, single quotes, etc. #!/bin/bash... (4 Replies)
Discussion started by: spacegoose
4 Replies

3. Shell Programming and Scripting

If expression syntax

Hello there! I am having some difficulties in the syntax of a script. I am a newbie :) What i want is a script that copies 1 or more files to a destination folder with a question if file allready exists if ($#argv > 2) then if (-d $argv($#argv)) then foreach x ($*) cp -i $x to... (4 Replies)
Discussion started by: ennui92
4 Replies

4. Shell Programming and Scripting

if: expression syntax error in gawk

I'm pretty new to shell scripting, but I am catching on quick. I did see one of the stickied threads about the csh, and I think this is relevant, but I don't understand enough to make a decision based on it. So as you'll see below, I use the |csh pipe, and if that is not correct, I'm certainly... (2 Replies)
Discussion started by: macman104
2 Replies

5. Shell Programming and Scripting

Confusion about FOR LOOP syntax between Bourne and BASH shell. Please see.

for (( i=1; i<=3; i++ )); do for (( j=1; j<=3; j++ )); do for (( k=1; k<=3; k++ )); do echo $i$j$k done done done Will the above code work on a BOURNE shell? As far as my understanding is, if I am writing the above code in a file..say lol.sh and then running it through the terminal using... (7 Replies)
Discussion started by: navienavnav
7 Replies

6. Shell Programming and Scripting

if expression syntax error

#! /bin/csh set umr=UMR foreach i ( `ls`) set file_nm=$i set bh_nm=`echo $file_nm | cut -d"_" -f2` if($bh_nm !=$umr) then { set bh_ext=`echo $file_nm | cut -d"_" -f4` set bh_num_nm="$bh_nm $bh_ext a .txt" mv $file_nm $bh_num_nm } ... (1 Reply)
Discussion started by: jdsignature88
1 Replies

7. Shell Programming and Scripting

syntax error for if statment test expression

Hi what's the correct way of writing if 1)if "$time_diff" -gt 5 then echo "killing hung process \n" fi 2)if test $time_diff -gt 5 then echo "killing hung process \n" fi where -time_diff=$(($Sam - $current_min)) and current_min=`date +%M` infact both are giving Syntax... (1 Reply)
Discussion started by: Anteus
1 Replies

8. Shell Programming and Scripting

Awk if elseif syntax error

Below is the code. nawk -F "|" 'FNR==NR{a=$3 OFS $4 OFS $5 OFS $6;next} {\ if ($5 in a)\ {print $1,"ABC",$5,"I",a, $2,$3,$4 OFS OFS OFS OFS OFS OFS OFS OFS $2"-"$3"-"$4} ; \ elseif ($5=="g")\ print $1,"ABC",$5,"I",$5 OFS OFS OFS OFS $2,$3,$4 OFS OFS OFS OFS OFS... (8 Replies)
Discussion started by: pinnacle
8 Replies

9. Shell Programming and Scripting

Expression syntax

Hello, I am getting Expression syntax message and I cannot figure out what's wrong. This program will print all the odd numbers between 0 and the one the user enters, in cshell. I think my logic is either good or almost there, but i dont see the errors. I think it could be the spaces between... (1 Reply)
Discussion started by: morava
1 Replies

10. Shell Programming and Scripting

One more expression syntax error

HI again, still working on the assignment, which is really hard given we just started unix 4 weeks ago. This script should change the permission for the user depending if its x, w or r, to the opposite. When i try to run it, I am getting expression error. Can you spot where the problem is? I really... (3 Replies)
Discussion started by: morava
3 Replies

Featured Tech Videos