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.
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 11:07 AM..
Reason: code tags, see PM
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:
... 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:
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 10:50 AM..
Reason: added note
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:
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:
Last edited by gary_w; 09-13-2011 at 10:49 AM..
Reason: I see your requirements changed from the original post, so this example is no longer valid.
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.
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.
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)
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)
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)
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)
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)
#! /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)
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)
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)
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)