How to assign variables in a csh script and used them as arguments for that same script?
Asked Answered
B

2

6

Good day,

Hoping for the kind help of anyone here, thanks in advance. I have T.csh which looks like this:

#! /bin/csh

set a="01 02 03 04 05 06 07 08 09 10 11 12 13"
set b="14 15 16 17 18 19 20 21 22 23 24 25"
set c="01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25"

set X = `grep $1 EOL.txt | head -n1 | cut -d- -f1`
printf "$X\n$2\n$3\nYYYY\n1\nN\n"

The variables a,b and c are optionally used as the 3rd argument in the printf line. The problem is, whenever I try to run the script, it showed undefined variable. These set command lines are working whenever I assigned them interactively, but inside the script, it seems to not work. Perhaps I need to initialize it but could not figure out how. Just new to this programming thing, I hope someone here can help me. Thanks a lot in advance.

Here are the sample execution and error for your reference:

CAT-46{bc2}40>set a="01 02 03 04 05 06 07 08 09 10 11 12 13"
CAT-46{bc2}41>./T.csh 4773 XXXX.XX "$a"
62
XXXX.XX
01 02 03 04 05 06 07 08 09 10 11 12 13
82869
1
N
CAT-46{bc2}42>unset a
CAT-46{bc2}43>./T.csh 4773 XXXX.XX "$a"
a: Undefined variable
CAT-46{bc2}44>

If i set the variables manually,it's OK, but when I called for it from the script, its flagging undefined variable error.

Mike

Bedew answered 31/1, 2017 at 2:37 Comment(2)
Paste the exact error you are seeing into your question.Askew
AND show a useage that includes $a, $b, $c. Based on another Q yesterday for csh, I believe that csh chokes on spaces in values for variable assignments. (even when they are quoted). So make sure you can make a simple use of one of those vars (with spaces in them) before spending a lot of time on this. See grymoire.com/unix/csh.html for a reasonable csh tutorial and discussion of why you probably want to use bash if you can. Good luck.Incapable
D
3

I post another answer because a comment is too short. Look at the following.

I have a script named /tmp/T.csh:

#!/bin/csh
set a="blah"
echo $a
  1. My shell is bash; I type /tmp/T.csh: result is blah (csh executed the script).
  2. Still in bash; I type unset a; /tmp/T.csh $a: result is the same.
  3. Still in bash; I type . /tmp/T.csh: no result (bash executed the script).
  4. I type csh; now I am in csh.
  5. I type /tmp/T.csh: result is blah (of course).
  6. I type /tmp/T.csh $a: "a: Undefined variable"
  7. set a = something
  8. /tmp/T.csh $a: blah
  9. echo $a: something
  10. unset a
  11. echo $a: "a: Undefined variable"

I replicated all you did; hope this helps. You get an error for what you wrote on the command line, not for the content of your script. Even a simple echo, as you can see here above, gives an error if you on the command line refer to a variable which does not exist.

Disinfection answered 31/1, 2017 at 8:22 Comment(5)
does that mean I cannot call on the values of a,b and c unless i set them interactively?Is there another way of calling these variables whenever I need them as script argument since I already set them in the script?Bedew
I don't understand what you want. Consider that a csh script has its local variables, and you pass to it arguments from command line. May be that setenv is your friend, read about it or, as said previously, use a more firendly shell.Disinfection
ok, my choice of commands could be at fault here, what I really want is to assign certain group of numbers (same as with my examples) into variables, for this case a,b and c to be used later as argument to my script. I thought set can do the trick. Any idea how I can do that? employing other shell type Im afraid is out of the question and as much as possible, no interactive setting of variables..Bedew
same error @linuxfan, undefined variable also when executed. Any other idea for this?Bedew
@Bedew for me works. Your script can use $1, $2... if args are in command line, or $varname if they are already set in the environment. Before running the script, you must use setenv to set environment. Use setenv always, and do a lot of echo. You will understand. Or, post another question with a full session, expected result and real result.Disinfection
D
0
prompt> unset a
prompt> ./T.csh 4773 XXXX.XX "$a"

The first command, "unset a", deletes the variable. In the second command you try to read the variable (on the command line!). That is why csh complains.

Disinfection answered 31/1, 2017 at 6:52 Comment(3)
hi @linuxfan, i just showed on that example that the set command is working on my shell. Inside T.csh, i also declared that same set command, so im expecting it can be called of as well when I execute it but its not.Bedew
No. The set does work in T.csh (provided that csh is really in /bin - are you sure?). But if you, interactively, do "unset a" and then "blabla $a", that can not work.Disinfection
No unsetting of variables to be done for this case. As for the csh,its in /bin. Any idea why it's showing undefined variable when T.csh is executed though the declaration was made in the script?Bedew

© 2022 - 2024 — McMap. All rights reserved.