Strange ls error when ls not used

September 28, 2010 at 19:46:01
Specs: Windows XP
I have a very simple c-shell backup script designed to copy a certain selection of folders to a backup folder. I've tested it and as far as I can tell it does what it's meant to, however I get some strange error messages. Code is at the end of this post.

The first line of my script is " #! /bin/csh -f " but I get this error:

bash-3.2$ myscript
bash: myscript: command not found

"csh myscript" successfully runs the script, but returns:

ls: No match.
TestFolder has been backed up.
Files were backed up to xxxxxx

What on earth is generating the ls error?


My code for 'myscript':

#! /bin/csh -f

set flist = (TestFolder $argv)
set d = `date +%d-%m-%y_%H:%M:%S`

mkdir ~/Backups/Backup_$d
set dir = ~/Backups/Backup_$d

foreach i ($flist)
if (-e $i) then
cp -r ~/${i} $dir
echo "$i has been backed up."
else
echo "Error! $i does not exist and was not backed up."
endif
end

echo "Files were backed up to ${dir}"

guest


See More: Strange ls error when ls not used

Report •

#1
September 28, 2010 at 20:00:58
With the space, the script is not defined as a csh shell script - but a bash script.

Eliminate the space:

#! /bin/csh -f

should be:

#!/bin/csh -f


Report •

#2
September 28, 2010 at 22:08:46
Nope. Doesn't help. Still getting the same errors.

guest


Report •

#3
September 29, 2010 at 02:20:00
Could it have anything to do with the fact that I'm running the script in the user directory, i.e. the same one that contains the .cshrc and .bachrc files?

I'm getting these same errors with any script I run in this directory, regardless of content. For example, script called 'test1':

#!/bin/csh -f

set a = 1
echo $a

Gives me this:

bash-3.2$ test1
bash: test1: command not found
bash-3.2$ csh test1
ls: No match.
1

Same again if I remove the first line of code altogether.

Any ideas what could be doing this?

guest


Report •

Related Solutions

#4
September 29, 2010 at 09:13:31
First, I tried your original myscript and it worked fine.

Second, I cannot duplicate your problem. I can tell you that since you are providing a shell invocation, there is no reason to execute the command

csh myscript.

Executing the script without the csh should work:

myscript

But I doubt that this is your problem. You can try it and see what happens.

Also, by default, the PATH variable is set so you don't search your home directory. What happens if your present directory is where myscript exists and execute this:

./myscript


Report •

#5
September 29, 2010 at 19:19:03
Typing ./myscript into the command line gave me a 'permission denied' error, however what you said about the PATH variable was ringing some bells so I opened up my .cshrc file and added the directory I was using to this variable. It now works fine. Typing myscript or test1 into the command line will run the script without any errors, be it ls or anything else.

guest


Report •

#6
September 29, 2010 at 19:53:08
Also, since the version of my backup script I posted above is embarrassingly crude and inefficient, I thought I might share the final version for anyone who's interested

Also, Nails, thanks for your assistance with the coding problems I've been posting lately. I'm a real beginner when it comes to c shell and I've only just discovered the power of scripting but I've really been getting into it and I'm starting to enjoy myself. :)

Anyway, the code: Now goes by the name of 'runbackup'. It assumes the home directory contains a folder called 'Backups', and can be run in three different ways:
- By default (i.e. with no arguments) it will backup a list of essential folders.
- With the argument 'all' it will backup a list of all folders that could possibly contain anything important.
- Or a custom list, so it will just backup the list of folders/files given as inputs.
The script will first check the existence of the things to be backed up, and abort if there's issues so that you can check your spelling and try again.
If everything's ok it'll create an appropriate folder in the backups folder, which contains the current date in its name.
Finally, it'll copy all specified files/folders to this backup directory, and exit with a message telling you where to find your backup.
I wrote this to avoid the issues of accidental use of the rm command or overwriting important files.
Still pretty simple as backup scripts go, and it's not very general since it's just customised to my system, but I'm quite proud of it. :)

#!/bin/csh -f

if ($#argv == 0) then
set flist = (Folder1 Folder2)
else if ($1 == all) then
set flist = (Folder1 Folder2 ... FolderN)
else
set flist = ($argv)
endif

set q = 0
foreach a ($flist)
if (! -e $a) then
echo "Error! $a does not exist."
set q = 1
endif
end

if ($q == 0) then

set d = `date +%d-%m-%y`

if (! -e ~/Backups/Backup_${d}) then
set dir = ~/Backups/Backup_${d}
mkdir $dir
else
set j = 2
while (-e ~/Backups/Backup_${d}_no${j})
@ j ++
end
set dir = ~/Backups/Backup_${d}_no${j}
mkdir $dir
endif

foreach i ($flist)
cp -r ~/${i} $dir
echo "$i has been backed up."
end

echo "Files were backed up to ${dir}"

else
echo "Errors detected. Backup aborted."
endif

guest


Report •

Ask Question