Undefined variable in mv script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Undefined variable in mv script
# 1  
Old 09-27-2010
Lightbulb Undefined variable in mv script

Hello,

Code:
#this script renames schematic file for model year change
 
#my = model year prefix
my="lsd"
for i in 'ls lsb*'
do
old=$i
new='echo $i | cut - c4 - c30'
mv $i $my$new
end

i: Undefined variable

What am I missing or doing wrong?

Last edited by Neo; 09-27-2010 at 05:04 PM.. Reason: code tags
# 2  
Old 09-27-2010
The shell cannot expand the * symbol inside ' ', also lose the ls....
Code:
#change
for i in 'ls lsb*'
# to
for i in lsb*

# 3  
Old 09-27-2010
I think the last bit should be 'done', not 'end'.

And "for file in `ls *`" is a useless use of backticks, you can just do "for file in *" and it'll do the same thing without ls.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 09-27-2010
Also, for starters:

Code:
my="lsd"

Code:
for i in 'ls lsb*'

Is this right??
This User Gave Thanks to Neo For This Post:
# 5  
Old 09-27-2010
Hi.

The error looks C-Shell-like.

If I run this in C-Shell I get lots more errors:

Code:
my=lsd: Command not found.
for: Command not found.
do: Command not found.
i: Undefined variable.

The syntax - although not correct - looks more bourne shell. What shell is it supposed to be run in?
This User Gave Thanks to Scott For This Post:
# 6  
Old 09-27-2010
Try this

Code:
#this script renames schematic file for model year change

#my = model year prefix
my="lsd"
for i in `ls lsb*`
do
  old=$i
  new=`echo $i | cut -c4 -c30`
done
mv $i $my$new
end

This User Gave Thanks to jville For This Post:
# 7  
Old 09-27-2010
variable readibility

The full format of the CAD file is cs(ModelYearIdentifier)yyy-70030-123456. So, engineers use a baseline design and make modifications. I have to be my own admin in this regard. Cp and mkdir all go well.

I want to replace the ModelYearIdentifier in the filename. I'm told SED and Perl are over kill for the task at hand. The ModelYearIdentifier is always the 3 character following the 3 types of CAD files.

So, with the posts below, I will try the following;

Code:
for i in lsb*
  code block
done



---------- Post updated at 03:47 PM ---------- Previous update was at 03:20 PM ----------

% is my prompt = C shell on SunOS 5.8.

Code:
finger -m user name
/bin/csh

I still get the i : Undefined Variable.

Moderator's Comments:
Mod Comment Learn to use code tags...... or else, LOL Smilie

Last edited by Neo; 09-27-2010 at 05:28 PM.. Reason: code tags are fun :)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Undefined variable error

I am getting the error undefined variable even after following these steps #read name abcd #echo $name na: undefined variable (6 Replies)
Discussion started by: Vishawdeep
6 Replies

2. Shell Programming and Scripting

Bash shell script undefined array item value question

Hello, I'm new here. I test these expressions's value in my script : (in centOS 6 ) #!/bin/bash array='something' echo "############" echo ${array} echo ${array} echo ${array} echo "############" The output result is : ################# something something #################... (5 Replies)
Discussion started by: lingjing
5 Replies

3. Ubuntu

error LD: undefined variable while changing shell from bash to csh

Hi, i am a beginner in ubuntu. my default shell is bash. everytime i try to change the shell with command "csh", i get a message (probably an error message). after i get into c-shell, when i try to execute a c shellscript, then it showed the same message. any idea about what is this about or any... (1 Reply)
Discussion started by: Avinash Nayak
1 Replies

4. Emergency UNIX and Linux Support

Csh script with Undefined variable error

hi there I have this C shell script that was migrated from AIX to Linux, could someone please help me, I checked the syntax numerous times but I can't find out where the error is. The script is meant to find files older than 27 days and delete it #!/usr/bin/csh # ... (22 Replies)
Discussion started by: hedkandi
22 Replies

5. Shell Programming and Scripting

script help Undefined /illegal variable using cat

Hello group, Still fairly new at the whole scripting thing so be gentle. I'm trying to write a simple script that archives my log files into a master log broken into weeks of the year. My script runs fine up till the "cat" lines which I get a undefined or illegal variable name error. But... (2 Replies)
Discussion started by: dpreviti
2 Replies

6. Shell Programming and Scripting

Undefined variable and command not found

Below script cuts date part from the date entered by the user. #!/bin/csh echo 'Enter date in the format dd/mm/yyyy' read DATE DD=`echo $DATE | cut -c1-2` echo $DD; when debug with -x option , it works perfectly but without -x doesnot.:confused: $ sh -x unix_12.sh + echo Enter... (2 Replies)
Discussion started by: hiten.r.chauhan
2 Replies

7. Shell Programming and Scripting

Undefined variable error in csh script

Below csh script gives error: Undefined variable:confused: #!/bin/csh $QUERY="netscape"; COUNT_NETSCAPE=${ps | grep -c $QUERY}; echo $COUNT_NETSCAPE; when run gives error adroit:/home/seo/hitendra 64 ] ./unix_6.sh QUERY: Undefined variable. What is the root cause of the... (3 Replies)
Discussion started by: hiten.r.chauhan
3 Replies

8. Shell Programming and Scripting

undefined variable error

Hi all, Im using a script which contains read command.. the script works perfectly but when I alias the script it gave "undifined variable" after I enter the read command input (variable)... Does any one know why ? (4 Replies)
Discussion started by: yahyaaa
4 Replies

9. Shell Programming and Scripting

bash: "undefined variable" and pipe

Hi, haven't found anything about this through searching, so may be a new topic: when doing this: set -o nounset set -o errexit find . -name "*.lib" | while read library; do echo ${libary} done echo "after while" I expect the script to exit within the while loop (because of nounset and... (6 Replies)
Discussion started by: nagaidhlig
6 Replies

10. UNIX for Dummies Questions & Answers

Prompting for Input - Getting Undefined Variable

#!/bin/csh -f echo "Enter MEUPS User Id :-" read UID echo "You entered $UID" --------------------------------------- Whn executed, I get an error message "UID: Undefined variable" (1 Reply)
Discussion started by: Kartheg
1 Replies
Login or Register to Ask a Question