Question on using a variable in KSH


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Question on using a variable in KSH
# 1  
Old 09-08-2010
Question Question on using a variable in KSH

Hi all,

The below command tries to copy ".tgz" instead of "hello_test.tgz" -- It seems as if the underscore gets in the way. I tried with different ways of using quotes, with no luck, unfortunately...it's probably very simple, but may I ask how this would be done:

How would the below be fixed to copy "hello_test.tgz" instead of ".tgz"?
Code:
NAME=hello
cp $NAME_test.tgz /tmp/$NAME_test.tgz

Thanks very much
cg
# 2  
Old 09-08-2010
By using:

Code:
cp -- "${NAME}_test.tgz" "/tmp/${NAME}_test.tgz"

Or just:

Code:
cp -- "${NAME}_test.tgz" /tmp

This User Gave Thanks to radoulov For This Post:
# 3  
Old 09-08-2010
The problem is that underscore is a valid character in a variable name.
The shell is substituting $NAME_test not $NAME .
Surrounding the variable name with braces is the solution and many scripters do this routinely.
Following on from radolouv is is also conventional to put double quotes round strings whether or not it is mandatory.

Code:
NAME="hello"
cp "${NAME}_test.tgz" "/tmp/${NAME}_test.tgz"

This User Gave Thanks to methyl For This Post:
# 4  
Old 09-08-2010
Thanks so much...hm, for now on (no matter what--it looks cleaner anyway) I'll use double-quotation marks. Thanks so much for your help!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Escaping a variable in ksh

I am trying to echo a variable exactly to a script- echo "${var1} ${var2} >> output.output Gives me a blank file. I would like output.output to basically say: ${var1} ${var2} I think I need to use a special escape character for variables. Am I right in assuming that, and is it the... (8 Replies)
Discussion started by: jeffs42885
8 Replies

2. Shell Programming and Scripting

Variable to command to Variable Question KSH

Hello, First post for Newbie as I am stumped. I need to get certain elements for a specific PID from the ps command. I am attempting to pass the value for the PID I want to retrieve the information for as a variable. When the following is run without using a variable, setting a specific PID,... (3 Replies)
Discussion started by: Coyote270WSM
3 Replies

3. Shell Programming and Scripting

variable substitution in ksh

Hi I have a variable BIT1 which holds some value. Is there a way to retrieve the value of this variable indirectly via another variable, lets say SUBSET_BIT_NUM=1, so the call will look something like this: sundev1 $ echo ${BIT${SUBSET_BIT_NUM}} ksh: ${BIT${SUBSET_BIT_NUM}}: bad substitution ... (3 Replies)
Discussion started by: aoussenko
3 Replies

4. Shell Programming and Scripting

checking variable has value - ksh

hi all, i have a shell (ksh) script where i am reading line by line from a grep command and i wanted to check if the grep command actually returned something or was null. i can do this by using -z : if ; then ..... but this forces me to do something when $myVariable is null when i... (3 Replies)
Discussion started by: cesarNZ
3 Replies

5. UNIX for Dummies Questions & Answers

ksh question

How can I know if my system has ksh feature? #!/usr/bin/ksh Which command we allow me to see? Please advise! (1 Reply)
Discussion started by: bobo
1 Replies

6. Shell Programming and Scripting

KSH variable substitution

Hi folks Please let me know if anyone knows how to handle this. My KSH script -> testscript.ksh cmd=$1 ENV="devl" echo $cmd This is how I call the script ./testscript.ksh 'ps -ef | grep br$ENV' How do I get this to print the below text i.e $ENV should be substituted with the value... (5 Replies)
Discussion started by: tipsy
5 Replies

7. Shell Programming and Scripting

ksh - get stdout name as variable

Writing a ksh script. If someone starts a process with: test.ksh > date.log How can I grab 'date.log' name as a variable in test.ksh? I need to get the 'date.log' name (not the contents) as a variable...without entering something like 'test.ksh date.log > date.log' (4 Replies)
Discussion started by: mhcueball2
4 Replies

8. Shell Programming and Scripting

ksh: A part of variable A's name is inside of variable B, how to update A?

This is what I tried: vara=${varb}_count (( vara += 1 )) Thanks for help (4 Replies)
Discussion started by: pa3be
4 Replies

9. UNIX for Dummies Questions & Answers

ksh on HP-UX -- variable expansion

We have a script that runs in ksh on HP-UX 11.11. It takes three arguments. The last argument can be a filename or wildcard character. For example: script -s hello -t goodbye '*.d*' In a case such as this, I would wrap single quotes around the final argument because I dont want the shell to... (4 Replies)
Discussion started by: dangral
4 Replies

10. Shell Programming and Scripting

Using a variable list in ksh while

Hey all, been so long since I've done any KSH scriptting, I've forgotten exact sytax. 2 things, 1) I need to dot in an environment file that is a list of server names 2) I need to loop through though server names in a while loop, or until loop. Here's roughly what I have and trying to hammer out... (2 Replies)
Discussion started by: jpeery
2 Replies
Login or Register to Ask a Question