Thanks for the reply amsct. For a while I thougt this was the answer to my problem
But your code below, is just a variation of typing the text of the file name into the script by hand. It does work for turning the hand typed text in to a variable that cp can read and I think it will be useful in the future for things that are defaults that need to be run the same as they would be from the command line.... but, it does not solve my problem. I did not know that single quotes used like that, made a diifference in the output and I will play with it in the future and look for other uses, so thanks for the information.
Quote:
Originally Posted by amsct
The processing flow of the "problem" is this:
1 - I read the contents of the directory into a variable.
2 - That variable is read by the for loop.
3 - The individual file names are provided as a variable by the for loop one at a time.
4 - I use that variable to cp the old file name to the new.
In other words my starting point for accessing the original file name, is always a variable returned by the for loop.
Somehow I need to turn that variable ( or a processed version of it ) into a version that will work with cp from inside the script.
Perhaps there are some changes I can make in the loop or there are other ways of reading in the file names to the loop.
But note that a for * type of loop, directly accesing the directory, will not work because the file names contain illegal characters and spaces.
This is the results of testing the two suggestions presented so far.
(I hope this is not a double post. I posted this earlier but it never appeared in the forum...)
As long as the exact block is entered into the script, all of the methods below will copy the old file (with the backslash) to the new file.
NONE of them will work when the oldname variable is read from the directory or passed from the "for loop" -- even when oldname is EXACTLY the same as any of the text assigned to oldname variables below.
In the TWO examples above, oldname MUST be quoted, with single OR double quotes,
AND newname MAY or MAY NOT be quoted with single OR double quotes.
-----------
Does this make any sense to anyone. I can not see how the bash shell could distinguish between a variable created inside the script, from data inside the script AND a variable created inside the script from identical text passed in from outside the script.
The only thing I can think of is that I am reading in the filenames to the variable through a pipe and that starts a sub-process which might fold, spindle or mutilate the variable in some manner that causes this problem.
I am leaving the country for a week in about 36 hours. If I have time I will rework the script to where the file list is imported without using a pipe and see if that changes how the variables react.
If anyone has any other ideas I would appreciate hearing them.
I never did truly understand "exactly" what it was about how I was using variables that was the problem but I found a work around, so to speak.
First if you want to use a variable to hold a "bad" file name, or one with unusual characters in it, to be used with the cp command -- then process the filename the minimum amount possible. I was processing the file name to create a new name, then I had to process it some more to undo the first processing so I could get the original name back. Bad idea, something happened to the filename while doing all that processing and I could never get it back to exactly what it was before... So I set out to minimize the processing of the filename to be used in the variable used to copy the old filename to the new filename.
Second, bash does its own processing of special characters and spaces when the cp command is run. Do not try to help it by adding escape characters (backslashes) to the variable. You have to deliver the name to the variable "exactly" as it appears when you use ls to display it. Then if the variable is quoted with double quotes, then bash can properly process it -- no matter what characters are in the file name.
Even though you can type the filename in by hand and add backslashes to the name and when you run the cp command from the script, it will work -- that ability will not transfer to a variable. Putting that same text with backslashes into a variable does not work because the variable does some processing "before or as" it expands the variable when the cp command is run. That "extra" processing produces something that is different than what was put in the variable, usually it adds backslashes and when it does, it does not match the "real" filename and can not copy it to the new filename.
Here is how I was originally processing the filenames. I was creating a list of filenames and storing them as a variable. Because the variable "fed" a for loop, I had to remove any spaces before I used the variable (for loops will not work with spaces in the filenames). I created the "filename list" variable and converted the spaces to % at the same time. I then separated the filenames into the basename and extension and stored them in variables. When I finished I reassembled this to get the original filename's basename and extension and removed the %s that I had inserted previously
This was too much processing. I ignored the KISS rule. The final cure was use the entire original filename with the % added -- as provided by the for loop -- instead of breaking it into basename and extension and having to reassemble all that back into the original file name. This allowed me to use the original filename with no processing other than removing the % symbol which I did at the same time I reassigned it to the final variable used by the cp command. Then all I had to do was quote the variable and everything worked.
My filename repair script works perfectly now and can repair filenames with leading and trailing spaces, backslashes, single quotes and any other characters that I have tried so far except the % symbol. There are two problems with % in the filename. When they are assigned to the variable storing the filenames bash turned % into %%, then the script turned all spaces into % and things went downhill from there. I decided to leave things alone and not try to process files with % in them, (files with % in them are very rare anyway).
I hope this makes sense, and can help someone in the future.
Hi,
Very good wishes to all!
Please help to provide the shell script for generating the record counts in filed wise from the .csv file
My question:
Source file:
Field1 Field2 Field3
abc 12f sLm
1234 hjd 12d
Hyd 34
Chn
My target file should generate the .csv file with the... (14 Replies)
Hi,
In sftp script to get files, I have to rename all the files which I am picking. Rename command does not work here. Is there any way to do this?
I am using #!/bin/ksh
For eg: sftp user@host <<EOF
cd /path
get *.txt
rename *.txt *.txt.done
... (7 Replies)
#!/bin/bash
#
name=$1
type=$2
number=1
for file in ./**
do
if
then
filenumber=00$number
elif
then
filenumber=0$number
fi
tempname="$name""$filenumber"."$type"
if (4 Replies)
HI,
I am trying to implement a simple shell script program that does not make use of ls or find commands as they are quite expensive on very large sets of files. So, I am trying to generate the file list myself. What I am trying to do is this:
1. Generate a file name using shell script, for... (2 Replies)
I am new to Shell Scripting and need some help.
The following batch job has been failing for me due to the .nfsxxx files in use. I need to know how to modify the following script to exclude the .nfsxxx files so this batch job will not fail on me. I have done lots of googling and keep coming back... (2 Replies)
Hi,
I am new to shell scripting.Please help me on this.I am using solaris 10 OS and shell i am using is
# echo $0
-sh
My requirement is i have source file say makefile.I need to extract files with extensions (.c |.cxx |.h |.hxx |.sc) from the makefile.after doing so i need to check whether... (13 Replies)
I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Dear other forum members,
I'm writing a script for my homework, but I'm scratching all over my head and still can't figure out what I did wrong. Please help me. I just started to learn about bash scripting, and I appreciate if anyone of you can point out my errors. I thank you in advance.
... (3 Replies)
Hi I am going to migrate our datawarehouse system from HP Tru 64 Unix to the Red Hat Linux.
Inside the box, it is running around 40 cron jobs; inside each cron job, it is calling other shell script files, and the shell script files may again call other shell script files or ctl files(for... (1 Reply)