Having problems copying a file from a script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Having problems copying a file from a script
# 1  
Old 12-29-2008
Having problems copying a file from a script

Hello. Complete newbie over here, and I'm hoping you can help me out with this problem.
The script copies a file to a directory within my home dir.
Permissions are ok and the source file exists. If I execute the cp command from the command line or hardcode the path/file name, it works.


(lines ommited for readability)
Code:
 
fileName=$1
 
source="/p/cpt/validation/HS/cpt/models/"$fileName"/rtl/cpt/dangling/list.report"
destination="~/averdugo/CPT_FILES/"$fileName".txt"
cp $source $destination

I think the problem is with the fileName variable because when I execute the program with sh -x option, I see:

Code:
 
+ source=/p/cpt/validation/HS/cpt/models/v081023/rtl/cpt/dangling/list.report
+ destination=~/averdugo/CPT_FILES/v081023.txt
 
+ echo /p/cpt/validation/HS/cpt/models/v081023/rtl/cpt/dangling/list.report
/p/cpt/validation/HS/cpt/models/v081023/rtl/cpt/dangling/list.report
 
+ echo '~/averdugo/CPT_FILES/v081023.txt' 
~/averdugo/CPT_FILES/v081023.txt
 
+ cp /p/cpt/validation/HS/cpt/models/v081023/rtl/cpt/dangling/list.report '~/averdugo/CPT_FILES/v081023.txt'
 
cp: cannot create regular file `~/averdugo/CPT_FILES/v081023.txt': No such file or directory

As you can see from the statements above, there are single quotes around the destination variable '~/averdugo/CPT_FILES/v081023.txt' I have no idea where they came from. Smilie

All your help is much appreciated.

Angel
# 2  
Old 12-30-2008
Hi,

Try this...
fileName=$1

source="/p/cpt/validation/HS/cpt/models/${fileName}/rtl/cpt/dangling/list.report"
destination="~/averdugo/CPT_FILES/${fileName}.txt"
cp $source $destination


You don't need the inside quotes, and you should use
${fileName}
whenever you embed a variable within double quotes and is up against other text.
Sometimes it works, but it is safer to do this method.
# 3  
Old 12-30-2008
~ is not valid in a traditional boure shell such as that found on Solaris.
# 4  
Old 12-30-2008
Quote:
Originally Posted by reborg
~ is not valid in a traditional boure shell such as that found on Solaris.
Thank you. Do you know of a workaround? Right now I have it working by substituting ~ for /usr/users/14afs, but hopefully there's something cleaner.

d'oh I guess I could use $HOME, right?

Last edited by verdugan; 12-30-2008 at 03:47 PM.. Reason: Remembered $HOME
# 5  
Old 12-30-2008
Quote:
Originally Posted by FlyingSquirrel
Hi,

You don't need the inside quotes, and you should use
${fileName}
whenever you embed a variable within double quotes and is up against other text.
Sometimes it works, but it is safer to do this method.
Thank you for that tip. So much to learn Smilie
# 6  
Old 12-30-2008
The ~ (current home directory) should not be inside any form of quotes.

From the command line, compare these two commands:
echo "~"
echo ~

If there are no spaces in $fileName we don't need any quotes, but we do need the curly brackets.
destination=~/averdugo/CPT_FILES/${fileName}.txt

If there are spaces in $fileName:
destination=~/averdugo/CPT_FILES/"${fileName}.txt"
# 7  
Old 12-30-2008
Quote:
Originally Posted by methyl
The ~ (current home directory) should not be inside any form of quotes.

From the command line, compare these two commands:
echo "~"
echo ~

If there are no spaces in $fileName we don't need any quotes, but we do need the curly brackets.
destination=~/averdugo/CPT_FILES/${fileName}.txt

If there are spaces in $fileName:
destination=~/averdugo/CPT_FILES/"${fileName}.txt"
Sweet. Thanks for that. Since $fileName will never have spaces, it's bye to quotes.

Angel
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dialog script for copying a file

Hello, I'm trying to write a script in Dialog for configuring a router. I basically need the user to select from a list of say 4 options, confirm the option and then copy the configuration files, depending on the choice to the HDD and then reboot (I'm hoping to boot from a USB stick). The... (0 Replies)
Discussion started by: pm77
0 Replies

2. Shell Programming and Scripting

Shell Script for copying text file to Excel Sheet

Hi, I want to write a program to copy a log file to Excel sheet. Excel sheet has four columns MethodName , Code , Description, Details and Time. I want to pick these info from text file and put it in excel sheet. here is how the text file looks - 04.17.2014 08:06:12,697... (1 Reply)
Discussion started by: hershey
1 Replies

3. Shell Programming and Scripting

Problems with script to find file names

Have a text file "test-array.txt" with contents below a23003 b23406 c23506 Tying to read the above file into an array and search for file-names in a directory TEST_DIR ,recursively with the above names in them. Example: If TEST_DIR has a files named xyxa_a23003_test.sql,... (4 Replies)
Discussion started by: gaurav99
4 Replies

4. Shell Programming and Scripting

Copying a string from a file using shell script

Hello everyone I am completely new to shell scripting in linux. I wan to write a script to search for a certain string from a .txt file and copy the string which apears just after tat searched string. Eg: in a file- try.txt , we have a line saying: "roses are red, so what do i do" I... (4 Replies)
Discussion started by: Kishore920
4 Replies

5. Shell Programming and Scripting

Bash Script Issues (If statement for file copying)

Writing a bash script for use with Geektool, pulls the battery info, and shuffles images around so that an Image geeklet can display the correct expression as the desktop background. (Eventually I intend to make it more intricate, based on more variables, and add more expressions) I'm extremely... (1 Reply)
Discussion started by: The_Ardly374
1 Replies

6. Shell Programming and Scripting

Problems coping a file in a shl script

I have the following in a shl script: SCRIPT_PATH="/u01/app/banner/test/skid/plus/"; FILE_PATH="/nfs/mercury/u03/banner/test/skid/log"; LIST_FILE_PATH="/u01/banjobs/TEST"; SCRIPT_NAME="szpcal1.sql"; FILE_NAME='new_applicant_list'; I want to copy the file FILE_NAME to LIST_FILE_PATH ... (10 Replies)
Discussion started by: rechever
10 Replies

7. Shell Programming and Scripting

Noob's 1st...bash-script for copying one file into many

I have one file "file.a.b.c-d.r" that I would like to use to spawn 4 other files: "file.a.b.1-A.r" "file.a.b.1-B.r" "file.a.b.1-C.r" "file.a.b.1-D.r" where the field "c-d" changes into my 1 and A-D. I was doing this manually at the prompt with > cp "file.a.b.c-d.r" "file.a.b.1-A.r" >... (13 Replies)
Discussion started by: WSUToad
13 Replies

8. UNIX for Dummies Questions & Answers

script copying the directory (or file) from server to my pc

Hello, I'm trying to create the shell script that: copy (or transfer) the directory from the unix server to my external hard drive (or hard drive) I've been serching this kind of thread here, but no luck so far. anyone can help me? Thank you. (2 Replies)
Discussion started by: myjwjw
2 Replies

9. Shell Programming and Scripting

problems with a script that outputs data to a file

First of all, im a total newbie to the point that i do not know what are the terms to search for my problem. I did however spend the rest of the day today trying to figure out what is wrong with my bash script. ive always thought that the best way to learn is to tackle a problem heads on. but at... (1 Reply)
Discussion started by: joeribut
1 Replies

10. UNIX for Dummies Questions & Answers

Problems with Copying Directory

Hi, i'm completely new to Unix. I'm trying to copy my "Pictures" folder, and all the folders under it, to a different folder "Photos". So say "Pictures" has a sub folder "2". what happens is "2" is copied to "Photos" like i want it to, but then a sub directory "tmp2" is made which... (3 Replies)
Discussion started by: brudnerx
3 Replies
Login or Register to Ask a Question