Cp command not working as expected in HPUX


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cp command not working as expected in HPUX
# 1  
Old 06-02-2015
Cp command not working as expected in HPUX

Hi, I'm having trouble with a simple copy command in a script on HPUX.

I am trying to copy a file and append date & time.

The echo command prints out what I am expecting..

Code:
echo "Backing up $file to $file.$DATE.$FIXNUM" | tee -a $LOGFILE

+ echo 'Backing up /home/efin/gp/source/kerf/verlist.pc to /home/efin/gp/source/kerf/verlist.pc.20150602.12345'

However, the copy copies the filename to $DATE.FIXNUM ..

Code:
cp $file $file_$DATE.$FIXNUM | tee -a $LOGFILE
+ cp /home/efin/gp/source/kerf/verlist.pc 20150602.12345

Thanks in advance
# 2  
Old 06-02-2015
You see the subtle difference between the two?
# 3  
Old 06-02-2015
Well, i won't tell you the difference... but

When using shell variables clustered like that together, use ${VAR} for everything to expand properly.

For instance $FILE_$NAME_$DATE will not expand correctly unless used in such manner e.g. ${FILE}_${NAME}_${DATE}


Hope that helps
Regards
Peasant.
# 4  
Old 06-02-2015
Oh, come on. Let's stop beating around the bush...
The shell knows that underscore (_) and period (.) are not the same thing, and you should recognize that too.

If a shell variable name is to be expanded in a string where the variable name is immediately followed by a character that can also be part of a shell variable name, the variable name must be surrounded by braces ({ and }) to keep the following character from being interpreted as part of the variable name. Valid simple shell variable names are strings of one or more alphanumeric characters and underscores where the 1st character is not numeric.

So, if three shell variables are to be expanded separated by underscores, you need braces to keep the underscores separate from the variable names. But if three shell variables are to be expanded separated by periods, the braces are not needed (but won't hurt if they are included).
# 5  
Old 06-03-2015
Thanks for the helpful and non helpful replies Smilie

Putting the braces around the variables sorted it. doh !

Apologies for my n00bness.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk command not working as expected

Following one line of awk code removes first 3 characters from each line but when I run the same code on another linux platform it doesn't work and only prints blank lines for each record. Can anyone please explain why this doesn't work? (31 Replies)
Discussion started by: later_troy
31 Replies

2. Shell Programming and Scripting

HPUX Syntax error at line 9 : `<' is not expected

Hi All, i'm getting this error, can somebody check? file name is like this 951957039339019_560119_000523_2w47_3060_0038_6002.log filename=$(basename "$1") extension="${filename##*.}" filename="${filename%.*}" IFS="_" read -r -a array... (2 Replies)
Discussion started by: charli1
2 Replies

3. UNIX for Advanced & Expert Users

Test -e not working as expected (by me)

I ran into the following and still do not understand entirely the rationale behind this. If someone could explain why things are as they are I'd be thankful. The following was tested on AIX 7.1 with ksh88, but i suspect that to be ubiquitous. In an installation routine i had to create a set of... (6 Replies)
Discussion started by: bakunin
6 Replies

4. Shell Programming and Scripting

Script not working as expected

Hi, I have prepared a script and trying to execute it but not getting expected output. Could you please help and advise what is going wrong. "If else" part in below script is not working basically. I am running it on HP-UX. for i in slpd puma sfmdb do echo "******\t$i\t*******" echo... (10 Replies)
Discussion started by: sv0081493
10 Replies

5. Shell Programming and Scripting

Read command not working as expected

I was trying to write a simple script which will read a text file and count the number of vowels in the file. My code is given below - #!/bin/bash file=$1 v=0 if then echo "$0 filename" exit 1 fi if then echo "$file not a file" exit 2 fi while read -n... (14 Replies)
Discussion started by: linux_learner
14 Replies

6. UNIX for Dummies Questions & Answers

-atime not working as expected

I need to sort through a volume that contains video files by access time and delete files that have not been accessed over x days. I have to use the access time as video files are originals that do not get modified, just read Testing commands on a local test folder... $ date Wed Sep 28... (10 Replies)
Discussion started by: canon273
10 Replies

7. Shell Programming and Scripting

Why this is not working in expected way?

total=0 seq 1 5 | while read i ; do total=$(($total+$i)) echo $total done echo $totalThis outputs: 1 3 6 10 15 0whereas I am expecting: 1 3 6 10 15 15My bash version: (4 Replies)
Discussion started by: meharo
4 Replies

8. OS X (Apple)

Cat command not working as expected

I've been trying to figure this out since last night, and I'm just stumped. The last time I did any shell scripting was 8 years ago on a Unix box, and it was never my strong suit. I'm on a Mac running Leopard now. Here's my dilemma - hopefully someone can point me in the right direction. I'm... (10 Replies)
Discussion started by: Daniel M. Clark
10 Replies

9. UNIX for Dummies Questions & Answers

Find command not working as expected

I have a script with a find command using xargs to copy the files found to another directory. The find command is finding the appropriate file, but it's not copying. I've checked permissions, and those are all O.K., so I'm not sure what I'm missing. Any help is greatly appreciated. This is... (2 Replies)
Discussion started by: mpflug
2 Replies

10. Shell Programming and Scripting

which not working as expected

Hello. Consider the following magic words: # ls `which adduser` ls: /usr/sbin/adduser: No such file or directory # Hmmm... Then: # ls /usr/sbin/adduser /usr/sbin/adduser # Now what? Unforunately this little sniippet is used in my debian woody server's mysql pre install script.... (2 Replies)
Discussion started by: osee
2 Replies
Login or Register to Ask a Question