LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 02-06-2012, 05:21 PM   #1
edi_corey
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Rep: Reputation: Disabled
cd command in shell script returns: No such file or directory


I have a script that I am running that copies files from one source folder to a destination folder.

The script is in a crontab file it's only function is to copy files
Within the script, I perform a change directory to the /tmp folder and execute the cp command to copy the file

cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"

This is the result:

[seeasown@sg3sacovn05 staging]$ ./movefiles.sh
: No such file or directory /tmp

Any idea why the cd command fails? the /tmp directory exists, in the correct case and spelling so that is not the issue. I've also tried
typing this using UltraEdit, notepad, and even vi editor on the linux box; Gives the same result.

Why is Linux not able to process the cd statement?

Thanks,

Corey
 
Old 02-06-2012, 05:47 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Quote:
Originally Posted by edi_corey View Post
I've also tried typing this using UltraEdit, notepad, and even vi editor on the linux box; Gives the same result.

Why is Linux not able to process the cd statement?
Linux can process the cd command, I promise!

You say you've tried UltraEdit and notepad. Was your first attempt using a file created with either of those programs?

Windows and Linux use different line endings for files. Using a file with Windows line endings in a Linux environment can cause some strange errors. You can convert Windows line endings to Linux line endings with the dos2unix command in Linux.

When you tried using vi, did you create a new file from scratch, or did you load one of the files created with UltraEdit/notepad? If you opened one of the existing files, vi may have detected the line ending format and worked around it. If you created the file from scratch--completely from scratch--in vi, then line endings would not be the problem.

If you are unsure, copy paste this code directly into a native Linux editor, save it, make the file executable, and run it:
Code:
#!/bin/bash

cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"
EDIT:
As an unrelated follow-on, be careful about commands and paths in cron files. The PATH environment variable for a cron job is very different than the PATH environment variable you have when typing commands at a terminal. It would be good for you to either (1) get in the habit of using full, absolute paths to any command you use in a cron script or (2) automatically adding a longer, more useful PATH declaration in your crontab file.

Last edited by Dark_Helmet; 02-06-2012 at 05:53 PM.
 
Old 02-06-2012, 05:56 PM   #3
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,631

Rep: Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696
Have you tried...

Hi Cory,
First, when you want to quote a script it is easy to make it clear if you use quote or code tags.

for example
Quote:
I have a script that I am running that copies files from one source folder to a destination folder.

The script is in a crontab file it's only function is to copy files
Within the script, I perform a change directory to the /tmp folder and execute the cp command to copy the file

cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"

This is the result:

[seeasown@sg3sacovn05 staging]$ ./movefiles.sh
: No such file or directory /tmp

Any idea why the cd command fails? the /tmp directory exists, in the correct case and spelling so that is not the issue. I've also tried
typing this using UltraEdit, notepad, and even vi editor on the linux box; Gives the same result.

Why is Linux not able to process the cd statement?

Thanks,

Corey
Second: You might want to use the command
Code:
 cat -vte ./movefiles.sh
to reveal any hidden characters that might be killing you.

Third: Why do a script from crontab with
Code:
cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"
instead of
Code:
cp /tmp/cputest_donotremove.txt /apps/opt/archive/tmp/demo/staging/encrypt01.tmp
cp /tmp/cputest_donotremove.txt /apps/opt/archive/tmp/demo/staging/encrypt02.tmp
anyway. There is no real reason for a script this short to change directory, nor any reason to double-quote the target in this case.

Fourth: you may find it to your advantage to make the first line of the script
Code:
 #!/bin/bash
to ensure that you are executing the script using the shell with the characteristics you desire.

I hope that this helps.
 
Old 02-06-2012, 06:29 PM   #4
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
You could also check the ownerships/perms on /tmp
Code:
ls -l /|grep tmp

drwxrwxrwt   6 root root  4096 Feb  7 10:21 tmp
 
Old 02-06-2012, 08:46 PM   #5
edi_corey
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hey folks, thanks for the replies, very helpful.

Third: Why do a script from crontab with
Code:
cd /tmp
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt01.tmp"
cp cputest_donotremove.txt "/apps/opt/archive/tmp/demo/staging/encrypt02.tmp"

instead of
Code:
cp /tmp/cputest_donotremove.txt /apps/opt/archive/tmp/demo/staging/encrypt01.tmp
cp /tmp/cputest_donotremove

Response:
The actual source path is /apps/fileport/opt/archive/tmp/demo/staging/ and the target is
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp

I just framed the question using tmp to avoid the response of someone asking to make sure the path is correct and in right case and so forth. So I just said /tmp to get around that.

The script when I use the cp command
cp /apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp /apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
is too long to fit on one line as you can see here and so for some God forsaken reason a /r gets appended to the target file so I end up with a file that looks like encrypt01.tmp? when I do an ls on the directory and it looks like encrypt01.tmp/r when I do a dir on the directory.
In other words the cp statment in the script has a return character and that becomes part of the target file name.
So to get around that whole mess I added the cd statement before the cp statment so that I could fit the cp on one line. I even tried making variables at the top of the script source=/apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp and then using $source and got the same problem No such file or directory so when I edit the script in vi, and copy the path to the clipboard for sanity check, it works fine but when it is in the script, I get the No such file or directory. Strange.

I used cat -vte ./movefiles.sh to see if there were any nonviewable characters in the script and there weren't any. No trailing spaces on any line just the $ after the last character in the cd statment. Also I used dos2unix after moving the file over from notepad > filezilla client > Linux server. No change in result.

One thing I noticed was the error message looked like: No such file or directoryapps/fileport (where it hacked off the leading slash, it's as if it's treating the slash in front of apps like an escape character or something. I will give #!/bin/bash a try and see if that changes anything .

No more all nighters on this one, I'm desperate!! Any ideas? What is a newb to do

Thanks fellas!
Corey
 
Old 02-06-2012, 10:44 PM   #6
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
The application PATH dose make a BIG difference
in the first post it looked like a REQUIRED system folder is missing
Quote:
No such file or directory /tmp
for that to be missing there is something VERY wrong someplace

however
copying /apps/fileport/opt/archive/tmp/demo/staging/
to
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
is a VERY different matter

do YOU ( as in your normal user account ) have read/write access to a folder mounted to /
namely "/apps "
in order to copy "/apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp "
into "/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp"

that script has to be owned by a user that HAS read/write access to "/apps/fileport/opt/archive/tmp/demo"
Folders in / are normally owned by root

chron should have the proper access set BUT what did YOU set the script as ?

who has the ownership of the script ?


Quote:
is too long to fit on one line as you can see here and so for some God forsaken reason a /r gets appended to the target file so I end up with a file that looks like encrypt01.tmp? when I do an ls on the directory and it looks like encrypt01.tmp/r when I do a dir on the directory.
what program are you using to write that ? MS windows notepad?

turn on the "show line numbers" for what ever it is
On MS windows i like SciTE
it is a nice FULL code editor
http://www.scintilla.org/SciTE.html



also you can add a \ ( escape it)to the end of one long line to continue on the next
example
Code:
cp /apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp /apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
------------------
would look like this 
-----------------
cp /apps/fileport/opt/archive/tmp/demo/staging/encrypt01.tmp \
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp

also if you use MS windows to edit a linux script
Windows WILL add all kinds of junk to the line endings
so USE "dos2unix"
and do not even think of using MS office to edit a script
 
Old 02-07-2012, 10:06 AM   #7
wpeckham
LQ Guru
 
Registered: Apr 2010
Location: Continental USA
Distribution: Debian, Ubuntu, RedHat, DSL, Puppy, CentOS, Knoppix, Mint-DE, Sparky, VSIDO, tinycore, Q4OS,Manjaro
Posts: 5,631

Rep: Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696Reputation: 2696
Misleading

Ok, you fooled me by providing sample lines completely different than the real lines, but many of the hints and comments above (both mine and from other contributors ) are still valid.

RE:
Quote:
The actual source path is /apps/fileport/opt/archive/tmp/demo/staging/ and the target is
/apps/fileport/opt/archive/tmp/demo/inbound/encrypt01.tmp
You might try something like this:
Code:
BASE=/apps/fileport/opt/archive/tmp/demo
cp ${BASE}/staging/encrypt01.tmp ${BASE}/inbound/encrypt.tmp
thus getting the advantages of full path names without generating very long command lines.

An alternate script might actually test and report issues, somewhat like this
Code:
#!/bin/bash
PATH=/bin:/usr/bin:/usr/local/bin
# expand on the path to reach all of the binaries you need, the above should be a good start.
BASE=/apps/fileport/opt/archive/tmp/demo
if [ ! -d ${BASE} ] ; then
   echo "I cannot find the base folder ${BASE}!"
   exit 1
fi
SRC=${BASE}/staging/encrypt01.tmp 
TRG=${BASE}/inbound/encrypt01.tmp
if [ ! -f ${SRC} ] ; then
   echo "The source file $SRC seems to be missing!"
   exit 2
else
   cp ${SRC} ${TRG}
fi
to present a clear message about what went wrong.

Last edited by wpeckham; 02-07-2012 at 10:07 AM.
 
Old 02-07-2012, 05:58 PM   #8
edi_corey
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
I want to sincerly thank all you guys! wpeckham's script solution worked like a charm. Nothing like getting the prompt back with no error messages after spending hours on this. Is there a good shell scripting book I can read or website to go to for more background on shell scripting?

I'm inspired to learn and share as you all have done and grateful the support and suggestions.

Thanks,

Corey
 
Old 02-07-2012, 08:08 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
These
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Enjoy
 
Old 02-10-2012, 06:01 AM   #10
edi_corey
LQ Newbie
 
Registered: Feb 2012
Posts: 5

Original Poster
Rep: Reputation: Disabled
Hey Chris Thanks, will definitely take a look at the links.

We can consider this thread closed.


Appreciate all the help.

Corey
 
  


Reply



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
help with a shell script that runs 'dos2unix' command on files in a directory, keerti Linux - Newbie 6 01-30-2012 10:06 AM
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 07:54 AM
Grep in bash script returns "No such file or directory", works manually gizza23 Programming 7 02-25-2010 04:37 PM
Using Variables in a Command that uses a Directory (Shell Script) CrimsonSkyZS Linux - General 4 01-11-2006 03:18 PM
Specifying target directory for command in bash shell script? spectrescape Programming 1 07-22-2004 05:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:39 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
Open Source Consulting | Domain Registration