I am getting an if statement error that I am unfamiliar with using CShell with arrays and if statements.
My file that I am reading is
FGF 101001011000
and my code is
#!/bin/csh
foreach year (1999 2000 2001)
foreach month (1 2 3 4 5)
echo "" > $year$month
if ($month == 1) then
set letter = F
else if ($month == 2) then
set letter = G
else if ($month == 3) then
set letter = H
else if ($month == 4) then
set letter = R
else if ($month == 5) then
set letter = D
else
print "Fail"
endif
@ year2 = $year % 100
awk '{split ($2,b,""); if (b["'$month'"] == 1) print $1"'$letter'""'$year2'"}' fileRead >> newFile
end
end
Is the value of month a string or a number and can i directly compare that value to the value of 1? Do i need to include set every time I use letter even though I have said set before? How do else if statements work in CShell? Do arrays in CShell work like they do in java? I know in java i would do month-1, however, from my research I have read that the array starts at 1 and NOT 0. Is this true? And can anyone tell me why I am getting an if error?
(Your headline says csh
, yet you've tagged this with rc-shell
. Note the low number of followers for rc-shell
. There is also the slightly newer tcsh
variant that has more interactive features than the original csh
and seems to still have a following)
Is the value of month a string or a number and can i directly compare that value to the value of 1?
csh
only has one type for data, and that is string. You can call external programs that understand stringified numbers, like bc
, dc
, and of course languages like awk
, perl
, python
, etc, but when you pass a value to the program to be worked on, it is a string. That said, you are using the @
"complex assignment" syntax of csh correctly.
If you just asking, can you subtract 1 from a variable named $month, then the answer is yes, using the @ month-=1
. What happens when $month = "02"?, Not sure, just do a test and you'll see ;-).
Do i need to include set every time I use letter even though I have said set before?
Yes, set
is part of the csh syntax for setting a variable
How do else if statements work in CShell?
?? Like any other language? Do you know how they work in another language? They only get evaluated (and the code inside executed) if the previous if
test has failed.
Do arrays in CShell work like they do in java? I know in java i would do month-1, however, from my research I have read that the array starts at 1 and NOT 0. Is this true?
I'm pretty sure that csh
is a one-based index. Unfortunately (or not), I don't have access to a csh either at home or at work, so I can't do a simple test. Just rely on the evidence that you find.
And can anyone tell me why I am getting an if error?
Without seeing the the exact error message you're getting, it will be hard to tell. Also having the context would be helpful. Fortuantely, you can see that for yourself by using the set -vx
debug/trace option available. If may be more obvious where the script is failing with that output. OR if the script refuses to run at all, comment out a large segment of the script, just to get it to run, then uncomment logical sections, until you find the offending section of code. (If you want to post the exact error msgs, then please edit your question to include that, don't post as a comment to this answer, please).
IHTH