LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script issue with "Undefined variable" (https://www.linuxquestions.org/questions/linux-general-1/script-issue-with-undefined-variable-727048/)

fuske 05-19-2009 09:08 AM

Script issue with "Undefined variable"
 
Hi!

I wrote a simple script that's get a variable by input.
I can run it fine by using sh script.

The problem i having is that when i try to execute it with ./script i get:

lala: Undefined variable.
datum=: Command not found.


The script:

#!/bin/csh -f
#
echo write something:
read lala

datum=`grep $lala /var/log/changelog | tail -1 | awk '{print $2 " " $3 " " $4 " " $5}'`

echo Datum......: $datum


I understand that sh make the script execute in a child environment or something like that.
But how do i make this script executable by ./?

kellinwood 05-19-2009 09:15 AM

Change the first line in the file to

Code:

#! /bin/sh
The interpreters sh and csh have different syntax.

fuske 05-19-2009 09:17 AM

Quote:

Originally Posted by kellinwood (Post 3545813)
Change the first line in the file to

Code:

#! /bin/sh
The interpreters sh and csh have different syntax.

:redface:
Thanks!

forrestt 05-19-2009 09:41 AM

You really DON'T want to use csh as a programming shell, but I know, "to each his own".

That being said, you aren't even writing the script in csh, you are using sh or bash syntax and then trying to send that to the csh interpreter.

Try this instead:

Code:

#!/bin/bash

echo write something:
read lala

datum=`grep $lala /var/log/changelog | tail -1 | awk '{print $2 " " $3 " " $4 " " $5}'`

echo Datum......: $datum

You will also need to run "chmod +x script" to make it executable.

If you REALLY want to use csh, then you will need to convert your script to use csh syntax (note, there is no "read" command in csh).

HTH

Forrest


All times are GMT -5. The time now is 03:04 PM.