Operating System - Linux
1748169 Members
4354 Online
108758 Solutions
New Discussion юеВ

Re: Problem with a variable in my short shell script

 
SOLVED
Go to solution
Chris Elmore
Frequent Advisor

Problem with a variable in my short shell script

Aloha Folks,

I am having some trouble with a basic shell script I am drafting and the error that is produced when I execute the script is this:

./Trp_Extract.sh[2]: TROUPERS^M: This is not an identifier.

The first 2 lines of the shell script sets a variable:

TROUPERS=/home/TROUPERS
export TROUPERS


The file is set to 777. Any ideas folks? Thanks in advance.

Very Respectfully,
Chris Elmore
"Life is love and love is life"
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Problem with a variable in my short shell script

Let the very 1st line of your script be:
#!/usr/bin/sh

export TROUPERS=/home/TROUPERS

It's always wise to use a "shbang" line in a script so that the system knows which executable to run your script against (POSIX/Bourne/Korn shell, perl, awk, ...).
If it ain't broke, I can fix that.
Chris Elmore
Frequent Advisor

Re: Problem with a variable in my short shell script

Aloha Mr. Stephenson,

Good to chat with you again! I tried using the shbang and this is what I get:

interpreter "/usr/bin/sh" not found
sh: Trp_Extract.sh: not found.

Any ideas what I'm doing wrong?

v/r,
Chris
"Life is love and love is life"
Chris Elmore
Frequent Advisor

Re: Problem with a variable in my short shell script

Aloha Clay,

Your export syntax was spot on! The variables are setting correctly now. I was going off of an older install doc and the format must not be compatible with our newer OS. Thanks a bunch! I still can't get the shbang to set though. Any ideas!

Your the BEST!

v/r,
Chris
"Life is love and love is life"
Matti_Kurkela
Honored Contributor

Re: Problem with a variable in my short shell script

Did you edit this script on a MS-Windows computer and then moved it to a HP-UX?

The '^M' suggests that there may be Windows-style end-of-line characters in the script that are confusing the shell.

If you use FTP to transfer the script from MS-Windows to HP-UX, remember to use ASCII transfer mode when transferring scripts. This will handle the line ending character conversion automatically.

Your script is apparently named "Trp_Extract.sh". Please run "dos2ux Trp_Extract.sh" to convert the line ending characters to Unix style.

By the way, it's a very bad habit to set file permissions to 777 whenever there's a problem. Someone may add a few commands of his/her own into your script... and when you run it, they are executed using your identity. A smart intruder uses this to build him/herself a back door to your account and then restore your original script.

755 is enough for a script. If it needs to be maintained by a group of people, then 775. Never 777.

MK
MK
Patrick Wallek
Honored Contributor

Re: Problem with a variable in my short shell script

Aloha,

/usr/bin/sh not found tells me that you might not be using HP-UX. If /usr/bin/sh were not on your HP-UX system then you would have a LOT of issues.

What operating system are you using?

Also, the line MUST be the very first line in the script and must be exactly as Clay posted it and it must start at the very beginning of the line.

# cat script
#!/usr/bin/sh
export VAR=somevalue
...
...
Chris Elmore
Frequent Advisor

Re: Problem with a variable in my short shell script

Aloha Folks,

You folks are the best! Sure enough, the source of my woes was my new windows ssh ftp client that I was using... it was embedding garbage windows esc sequenses in all of my scripts. I fixed them in vi and all is well again. You folks are a real credit to the forums and I humbly thank you each and every one, for your rapid and thoughtful responses. Please take care folks!

Very Respectfully,
Chris Elmore
"Life is love and love is life"