LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-05-2011, 10:40 AM   #1
User8472
LQ Newbie
 
Registered: Aug 2011
Posts: 6

Rep: Reputation: Disabled
Question TCSH script works on Windows 7, does not work on Fedora


I have written a tcsh script on a Windows 7 machine in order to perform a task on a machine running Fedora. After writing the script, I used cygwin to run it on the Windows 7 machine, and it worked exactly as intended. Then I moved it to the Fedora machine, and I got the following error:

Code:
While: badly formed number
Here is the beginning of the script:

Code:
#!/bin/tcsh -f
##
## Script to automate loading of A2 DAQ systems
##
##
echo "This program will attempt to automate the DAQ Control process"
set runNum = -1
set confirmNum = "x"
set runNote = "x"
set confirmNote = "x"
set select1 = -1
set confirm1 = "x"
set select2 = -1
set confirm2 = "x"
set select3 = -1
set confirm3 = "x"
set delay = "x"
#
# Set and confirm the run number
#
while ($runNum < 0)
	echo -n "Please enter the number of the current or most recently completed run: "
	set runNum = "$<"
	if ($runNum < 0) then
		echo "Error: Invalid run number"
	else
		while ($confirmNum != "y" && $confirmNum != "n")
			echo "You entered run number: " $runNum
			echo -n "Are you sure this is the correct run number (y/n)?"
			set confirmNum = "$<"
			if ($confirmNum == "n") then
				set runNum = -1
			endif
		end
		set confirmNum = "x"
	endif
end
Before the error, I see the string "This program will attempt to automate the DAQ Control process", but not "Please enter the number of the current or most recently completed run: ", so clearly the problem begins at the first "while"

Does anyone know what could be the reason for this problem, and how I may be able to fix it?
 
Old 08-05-2011, 11:19 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Well, I'm running
Code:
tcsh --version
tcsh 6.17.00 (Astron) 2009-07-10 (x86_64-unknown-linux) options wide,nls,dl,al,kan,rh,filec
on Slackware 64-bit 13.37.

I copied your shell program into daq.sh and executed it:
Code:
tcsh daq.sh   
This program will attempt to automate the DAQ Control process
Please enter the number of the current or most recently completed run: 21
You entered run number:  21
Are you sure this is the correct run number (y/n)?y
So, is that what you expect?

Perhaps you need to look at your source code with vim and clear out carriage returns?

Hope this helps some.
 
Old 08-08-2011, 06:32 AM   #3
User8472
LQ Newbie
 
Registered: Aug 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Yes, that is what this section of the script was supposed to do.

I have downloaded and installed vim, and am looking at the file now, but what exactly to you mean "clear our carriage returns"? I don't think I have any carriage return characters in the script, though there are a few newlines. And how would those cause a problem on one system but not another?

Also, I noticed you named your file "daq.sh" - my file is named "autoRun" with no file extension - could that be causing the issue?
 
Old 08-08-2011, 09:43 AM   #4
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,476

Rep: Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020Reputation: 1020
Windows text files terminate lines of text with a carriage return and a line feed (CR/LF); all Unix-like systems use only a LF to terminate text lines. The CR character can play havoc sometimes.
vim will show you control characters in a text file (a CR is a control character) that "shouldn't" be there. If you don't have any, so much the better -- just something to be on the look-out for. You won't see horizontal tab (HT) or line feed (LF) because they are supposed to be there, eh?

It's pretty common practice that a shell program source code file be named "something.sh" (as you would name a C-language source code file "something.c" and so on with other programming languages (keep in mind that shell programming languages, Bourne, Korn, C-Shell, BASH) are programming languages with specific grammar and syntax rules. It's also pretty common practice that all source code files that matter; i.e., they're going to be used for a while rather than just a one-off quickie, be stored in a source code control system; e.g., CVS, so they can be edited as necessary over time with changes stored in the source code control system.

The make utility will, by default, create an executable shell program from a source code file simply by executing
Code:
make name
where "name.sh" is the source code and the generated file will be "name" (there are hundreds of shell programs that are part of your distribution that are generated from source exactly like this).

In the case of your file, I saved it as daq.sh. If I
Code:
make daq
I can then simply enter
Code:
daq
and it'll execute (so I would not need the tcsh daq.sh, only daq.

If you're interested, take a look:
Code:
cd /usr/bin
file * | grep shell
You'll see a large number of files that are "POSIX shell script text executable" or something similar to that.

One of the reasons that people have stopped using C-Shell is that it does thing for you that you probably don't want done; e.g., if a C-Shell user has a .chsrc file in their home directory, every time you hit the return key to execute something the content of that .cshrc file is executed (and you don't want that to happen sometimes). It's hard to get around that and I'm not sure that using the -f option embedded in the source code file will prevent it (maybe doing
Code:
tcsh -f autoRun
may be an option). I abandoned C-Shell about 20 years ago so I really don't have much of a feel for what sort of naughty behavior it may get up to nowadays. The company I retired from a few years ago insisted that users' use C-Shell and used a system-wide ".cshrc" so they could not alter their environments (PATHs, defaults and the like). Caused problems, that, and took some gyrations to get around (as a developer, I used Korn Shell so that "magic" didn't affect my work). Keeping in mind that a child process inherits the parent processes environment in C-Shell, that may be something to look at.

About the only thing I can think of (and remember, I'm not a C-Shell programmer) is that you're getting something illegal from the command line when you enter the number at the prompt -- you might want to try displaying the entry to see what you're actually getting, say above the line
Code:
echo -n "Please enter the number of the current or most recently completed run: "           
        set runNum = "$<"
The only other thing I can think of is -- and this is kind of out there -- to enclose variables in braces so $runNum becomes ${runNum}. Might matter, might not, but the braces do a good job of isolating variables from any possible interference.

Bottom line, though, is this: my environment is Korn Shell, not C-Shell and your code appears to work as you expect on my system. That would lead me to conclude that you have some strange and wonderful environment settings getting done for (or, you know, to) you that are causing problems. Just ain't much else left. Take a hard look at your .login and .cshrc files and see what's going on in them. Also, from a shell prompt, try
Code:
env | sort | pg     (or | more)
and see what's what.

Then, do a one-liner (well, maybe two-liner)
Code:
#!/bin/tcsh -f
env | sort | pg
and compare the two -- instead of piping into pg, redirect output from the above into files and use diff to see if there's something biting you.

And, finally, do a quick re-write in Bourne, Korn or BASH and see what you get.

Hope this helps some.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
many tcsh aliases work in cygwin but not in fedora when defined in .alias file WrapperMaven Fedora 2 04-16-2011 02:54 PM
tcsh script: can you time parts of the script? BrianK Programming 7 02-02-2010 08:20 PM
rsync works differently under tcsh than bash? Vanyel Linux - Software 2 09-01-2009 10:55 AM
Script works in work dir but nowhere else Drutten Programming 7 10-01-2006 11:22 AM


All times are GMT -5. The time now is 08:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration