Shell Variables: Shell prompt OK, script, NO.

Shell Variables: Shell prompt OK, script, NO.

Post by Stephen Montgomery-Smit » Thu, 03 Jun 1999 04:00:00




> I'm just learning Unix in an environment which runs FreeBSD
> 2.2-971230-SNAP.  The shell is csh.

> I have a 'test' file which it seems won't set variables.  It contains:

> set logdate = `date "+%Y%m%d"`
> echo $logdate
> echo $logdate > test.dat
> (EOF)

OK, a script like this is run under sh, for which the correct syntax seems
to be
setvar logdate `date "+%Y%m%d"`
(from me reading the man page for sh just now).

Or you could put
#!/bin/csh
at the top of your file which tells your computer to run it using
csh.

--




University of Missouri-Columbia
Columbia, MO 65211
USA

Phone (573) 882 4540
Fax   (573) 882 1869

http://math.missouri.edu/~stephen

 
 
 

Shell Variables: Shell prompt OK, script, NO.

Post by Tom Kea » Thu, 03 Jun 1999 04:00:00




Quote:> I'm just learning Unix in an environment which runs FreeBSD
> 2.2-971230-SNAP.  The shell is csh.

> I have a 'test' file which it seems won't set variables.  It contains:

> set logdate = `date "+%Y%m%d"`
> echo $logdate
> echo $logdate > test.dat
> (EOF)

> The permissions are u:rwx, and when I run it as ./test, a blank line is
> echoed to the terminal screen.  The test.dat file contains only a blank
> line.

> Yet, if I execute each of those lines "live" from the terminal, it sets
> the variable correctly, echoes the value, then puts the value into the
> test.dat file.

> I'm guessing the problem is that the shell script is not storing or
> reading values from variables.

> What's going on here?

Try this in your script:

#! /bin/csh

set logdate = (`date +%Y%m%d`)
echo $logdate | tee test.dat

Refer to the manpage for csh, esp. the section "Expressions"
for a discussion on precedence of + % ` and ( as well as the
"Lexical structure", "Command substitution" and
"Quotations ..." sections.

Your own $HOME/.cshrc and $HOME/.login might afford some
helpful examples/hints.

You might also like to lurk in comp.unix.shell for some
very interesting and enlightening discussions on scripting.
If you post your question there, certain pedants would
recommend against naming your script "test" (there's already
a /bin/test) and/or for using csh for scripting at all (some
folks read a certain article in O'Reilly's "Unix Power
Tools" and take it as Gospel)

I won't bug ya, though  :)

If you're /really/ interested in scripting, i recommend getting
a copy of "UNIX Shell Programming" by Stephen G. Kochan and
Patrick H. Wood.  My copy is so old i don't know if its publisher
(Hayden Books) or ISBN are still valid.

HTH

cheers,
        tom

--
Applicants must have at least five years' experience with Windows 98 ...

        remove NO_SPAM. from address to reply

 
 
 

Shell Variables: Shell prompt OK, script, NO.

Post by Martin Dieringe » Fri, 04 Jun 1999 04:00:00



> I'm just learning Unix in an environment which runs FreeBSD
> 2.2-971230-SNAP.  The shell is csh.

> I have a 'test' file which it seems won't set variables.  It contains:

> set logdate = `date "+%Y%m%d"`
> echo $logdate
> echo $logdate > test.dat
> (EOF)

try

logdate=`date "+%Y%m%d"`
^      ^
no     no
set    spaces!

m.

 
 
 

Shell Variables: Shell prompt OK, script, NO.

Post by Shaun Rowlan » Fri, 04 Jun 1999 04:00:00




> > I'm just learning Unix in an environment which runs FreeBSD
> > 2.2-971230-SNAP.  The shell is csh.

> > I have a 'test' file which it seems won't set variables.  It contains:

> > set logdate = `date "+%Y%m%d"`
> > echo $logdate
> > echo $logdate > test.dat
> > (EOF)

> OK, a script like this is run under sh, for which the correct syntax seems
> to be
> setvar logdate `date "+%Y%m%d"`
> (from me reading the man page for sh just now).

This is what my "sh" manual page says:
--------------------------------------

     setvar variable value
             Assigns value to variable. (In general it is better to write
             variable=value rather than using setvar.  Setvar is intended to
             be used in functions that assign values to variables whose names
             are passed as parameters.)

So, yes you can use setvar but it is better not to.

Quote:

> Or you could put
> #!/bin/csh
> at the top of your file which tells your computer to run it using
> csh.

csh is "evil" IMHO.  Write shell scripts in ksh/sh/bash.  
--

IICF System Administrator       DL798
http://www.cis.ohio-state.edu/~rowland
 
 
 

Shell Variables: Shell prompt OK, script, NO.

Post by Tom Kea » Fri, 04 Jun 1999 04:00:00




Quote:>> Applicants must have at least five years' experience with Windows 98 ...

> I assume this is to prove that you can put up with a lot of crap, an
> unreliable, sloppy work environment, and logically impossible
> expectations.

Actually,i just occassionally see this requirement in career ads
posted, of course, by those endearing social benefactors: headhunters.  
Everytime i see it, it tickles my funnybone.  Sometimes i consider
applying to them as a lark, but i'm underqualified :)

cheers,
        tom

P.S.
I wonder if there's any free csh tutorials on the Web ... i haven't
tried looking, but something might turn up.

--

        remove NO_SPAM. from address to reply

 
 
 

1. How to pass a variable from a shell script to another shell script...

Hi,
         I have written a shell script 1............and internally its
calling another shell script 2........input to  redhat specific
chkconfig command... which executes and builds links in bootstartup
scipt directories (rc.d).

Actually i needs to pass a variable $DIRECTORY  of my command available
( can hard code but i am
setting it in the 1st script) to the 2nd script how to pass that... As
the setting is happening before the calling of
shell script2, i just tried like putting  echo "$DIRECTORY" into the 2nd
shell script, which is not working is there
a way to get the functionality.

shellscript1:
#!/bin/sh
---------
---------
---------
DIRECTORY="/sbin/ntp"
---------
---------
/sbin/chkconfig "shellscript2"
---------

shellscript2:
#!/bin/sh
-------
echo "$DIRECTORY"     <------put to see whether getting the value here
or not..

$DIRECTORY   -c ntp.conf          <------if it is so ,  i could use like
this.................

thanks,
srinivas.

--
office ph: 091-40-3607619

2. ftp question

3. flexible prompt in shells via shell-script

4. h323 in 2.4 kernel

5. export variables to calling shell in a shell-script

6. problem with processing speed on i486

7. 504 UUCP dials OK, SCOPPP receives garbage.

8. Expanding a shell variable that may contain shell variables.

9. c shell script; Howto use environement variables to build environment variables and take their value

10. file contents into variable, or global variable from shell script

11. handling SIGINT in shell scripts when executing another shell script.

12. Q: How can I have a shell script call another shell script...