Using variable with cp command in bash shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using variable with cp command in bash shell
# 1  
Old 12-01-2006
Using variable with cp command in bash shell

Hi,

I am trying to write script to copy some files(/ppscdr/cdrp2p/temp/) from one directory to another directory using shell script. see the script below,

Code:
#!/bin/sh -f
dir_name=20061105

        mkdir ${dir_name}
        cd /ppscdr/cdrp2p/temp
        pwd
        cp p2p${dir_name}*.* /ppscdr/cdrp2p/${dir_name}
        pwd


But id dosen't work properly (didn't copy the files).
See the error as below,

Code:
[root@cdrwebserver cdrp2p]# ./cp.sh
/ppscdr/cdrp2p/temp
cp: cannot stat `p2p20061105*.*': No such file or directory
/ppscdr/cdrp2p/temp

But there are some files in the /ppscdr/cdrp2p/temp directory starting with p2p20061105.

Can some one help me to find the resolve the error.
# 2  
Old 12-02-2006
Code:
p2p${dir_name}*.*

you have a file p2p20061105 file but you are using p2p${dir_name}*.*
why ? i could not understand...

just remove the '*.*" from the script..

I tested with the same it is working..

you will get desired result.
# 3  
Old 12-02-2006
Actually in my cd /ppscdr/cdrp2p/temp contains files as follows,

p2p20061105_1010003_0101.unl
p2p20061105_1010003_0102.unl
..
..
..
p2p20061106_1010003_0101.unl
p2p20061106_1010003_0102.unl
...
...
...
p2p20061130_1010003_0101.unl
so on....

What I want to do is create a new folder called $dir_name and coppy the file
which are starting p2p${dir_name}. Thats why I use the code as above.
# 4  
Old 12-02-2006
dir_name=20061106

mkdir ${dir_name}
echo ${dir_name}
cd /home/sri/tmp
pwd
ls -l p2p${dir_name}*.* #check 0/p this command then you come to know what is the problem
cp p2p${dir_name}*.* /home/sri/sri1/${dir_name}
pwd


above this is working perfectly as this is executed from /home/sri/sri1/scrit_name

it is creating a direcoty 20061106 and copy the *.unl file in it
# 5  
Old 12-04-2006
After I remove -f from the first line of the script (
#!/bin/sh -f) script wroked fine. I am confuse what is the problem with -f.

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to insert a string and variable at specified position in command in bash?

I currently have a loop that reads all .bam files in a directory (wont always be 4 like in this example, into $id. What I am trying to do, unsucessfully, is create specific new lines in an exsisting command using each $id. Each new line would be: --bam ${id} \ Tried p=$dir... (8 Replies)
Discussion started by: cmccabe
8 Replies

2. Shell Programming and Scripting

Need bash script to use a sed command as a variable

I need to be able to use a sed command as a variable in a bash script. I have the sed command that almost works the way I want it. the command is sed -n '/inet/,/}/p' config.boot This gets me this result: inet 192.168.1.245 } I need to get the IP address into a variable so I... (9 Replies)
Discussion started by: edlentz
9 Replies

3. UNIX for Beginners Questions & Answers

How do I assign the output of a command to a variable within a loop in bash?

In the else of the main if condition . else set lnk = $(readlink -f <path> | cut -d '/' -f7) echo "$lnk" if ] When I run the above on command line , the execution seems to be fine and I get the desired output. But when I try to assign it to a variable within a loop... (12 Replies)
Discussion started by: sankasu
12 Replies

4. Shell Programming and Scripting

Convert a string to variable in Bash shell

Hi All; I have 2 variable let's say $A and $B. These are actually some remotely executed command outputs. I captured these values in my local variables. Here is my problem. When I try to do some arithmetic operation with these variables, I receive an error message. Neither expr nor typeset -i... (3 Replies)
Discussion started by: Meacham12
3 Replies

5. Shell Programming and Scripting

Bash script set command to a variable

Hi, Will following set up work in bash script? I've got errors if assigning following binary command to a variable. But on the other hand, COMMAND="ls" works. Any explanation please? How can I assign binary command to a variable COMMAND then I can just call ${COMMAND}? COMMAND="rsync"... (3 Replies)
Discussion started by: hce
3 Replies

6. Shell Programming and Scripting

variable issue in a bash in a shell script

Hi, I am trying to write a PBS shell script that launches a bash process. The issue is that the bash process needs a variable in it and the shell script is interpreting the variable. How do I pass this as a literal string? Here is my code snippit: TMP=".fasta" FILEOUT=$FILE$TMP cd... (2 Replies)
Discussion started by: bioBob
2 Replies

7. Shell Programming and Scripting

Not able to store command inside a shell variable, and run the variable

Hi, I am trying to do the following thing var='date' $var Above command substitutes date for and in turn runs the date command and i am getting the todays date value. I am trying to do the same thing as following, but facing some problems, unique_host_pro="sed -e ' /#/d'... (3 Replies)
Discussion started by: gvinayagam
3 Replies

8. Shell Programming and Scripting

Error in setting PATH variable in bash shell

Hi, I am new to shell scripting.I tried adding an entry to the path variable like below export PATH=$PATH:/opt/xxx/bin But am getting an error invalid identifier /opt/xxx/bin Can someone tell me the error above and correct me . Thanks and Regards, Padmini (2 Replies)
Discussion started by: padmisri
2 Replies

9. Shell Programming and Scripting

Assign bash command to variable

Hi I am trying to write a function that needs to be able to assign the last run shell command to a variable. The actual command string itself not the exit code of the command. I am using the bash command recall ability to do this as follows: alias pb='ps | grep ash' ... (3 Replies)
Discussion started by: Moxy
3 Replies

10. Shell Programming and Scripting

bash variable - unix command

I want to just run this command from a bash script and put it in variable. date '+%M' gives the minutes in solaris. (2 Replies)
Discussion started by: photon
2 Replies
Login or Register to Ask a Question