Visit Our UNIX and Linux User Community


Hash (#) as a variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Hash (#) as a variable
# 1  
Old 05-16-2002
Hash (#) as a variable

Hi

I'm writing a cript that copies and renames files to a backup location. This script can be used in a "test" mode, whereby all the files that will be copied are listed. The problem is that the destination file will not yet exist, so I want to comment this part (I'm shure you'll see what I'm getting at from the example below!)

I have the following in the script:
Code:
if test "$control_code" = "cp";
 then
   echo ""
   echo "You have chosen $control_code"
   echo "Files are being copied."
   echo ""
   ext="/backup"
 else
   echo ""
   echo "You have chosen $control_code"
   echo "No files will be copied, only tested"
   echo ""
   control_code="ls -l"
   ext=#
fi  

...

echo "cp /dir/testfile /backup/testfile_1"
$control_code /dir/testfile $ext/testfile_1

echo "cp /u01/testfile /backup/testfile_2"
$control_code /u01/testfile $ext/testfile_2

...

Currently I get the result:
#/testfile_1: No such file or directory
#/testfile_2: No such file or directory

You can see that the hash (#) comment value is not recognised.

How can this be done???


Any help would be greatly appreciated!!!

added code tags for readability --oombera

Last edited by oombera; 02-19-2004 at 11:28 AM..
# 2  
Old 05-16-2002
Whoa.... your code is certainly interesting. I'm very surprised that it works as well as it does. I'm gonna reread my ksh book tonight to try to figure this out.

But, in the meantime, the answer to your question is:
eval $control_code /u01/testfile $ext/testfile_2
# 3  
Old 05-16-2002
Thanks - it worked!

Your comment got me a bit worried, as it sounds as though I'm doing something wrong...if so, PLEASE tell me!

(Note: I only posted part of the script!)
# 4  
Old 05-16-2002
The first thing that worried me is this sort of thing:
x=date
$x

It seems to me that the bourne shell used to barf on code like that. But ksh sure seems to take it in stride. I am used to doing:
x=date
eval $x

which did indeed work with the bourne shell. Since you are using an eval anyway, the point is kinda moot. But it's cool that this works and I'm gonna start using it.

But next we have this:
y=#

I would have expected that to be the same as:
y= # I just set y equal to nothing

Again, I can't make it fail, so I guess it's probably legal. But I sure can't convince myself of that by reading Dave Korn's book. A quoted pound sign can never start a comment, so I would go with
y="#"

which is, I think, somewhat more clear. And it may be a fluke that y=# currently works. If a new release of ksh was to break that, you would suddenly have a hard bug to find.
# 5  
Old 05-17-2002
Thanks Perderabo!
# 6  
Old 05-21-2002
Quote:
The first thing that worried me is this sort of thing:
x=date
$x
I use a similar type of substitution in a monitoring script:
Code:
[...]
lynx="/opt/lynx/bin/lynx"
site="www.company.com"

${lynx} -dump ${site} >/dev/null 2>&1
case $? in
[...]

It makes it easier for me to change, and makes the commands themselves easier to read for me...
 

Previous Thread | Next Thread
Test Your Knowledge in Computers #323
Difficulty: Easy
In most programming languages, the operator ++ is equivalent to the statement -= 1;.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separate a hash variable into 2 parts in Perl

Dear Perl users/experts, Could somebody help me how to solve my problem, I have a hash variable that I want to convert into dot file (graphviz). I know how to convert it to dot file but I need some modification on the output of the hash variable before convert it to dot file. Eeach key of... (1 Reply)
Discussion started by: askari
1 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

In Perl can i define a hash with value as variable?

Hi, Is it possible in perl to have a hash defined with variables as theirs key values, like: %account = ('username' => 'boy', 'password' => $password); Thanks (1 Reply)
Discussion started by: zing_foru
1 Replies

4. Shell Programming and Scripting

Perl: accessing reference to variable inside hash.

Below is hash which contains reference to variables: my %mandatoryFields = ( 1 => \$msgtype, 2 => \$switchtype, 3 => \$card_nbr, 4 => \$natv_tran_type_code, 5 => \$amt_1 ); This... (0 Replies)
Discussion started by: som.nitk
0 Replies

5. Shell Programming and Scripting

Variable with hash character

Hi Experts, I need to have a variable with value initialised to it.The value will possess a hash character with it. ex: var=reports#2 so how I am suppose to do this? (1 Reply)
Discussion started by: cnraja
1 Replies

6. UNIX for Advanced & Expert Users

Passing Hash variable in to sql query in perl

Hi Everyone, Can anyone help me how do i call hash variable in to sql query in perl. Please see the script below i have defined two Hash %lc and %tab as below $lc{'REFF'}='V_RES_CLASS'; $lc{'CALE'}='V_CAP_CLASS'; $lc{'XRPD'}='V_XFMR_CLASS'; $tab{'V_RES_CLASS'}='V_MFR_SERS';... (6 Replies)
Discussion started by: jam_prasanna
6 Replies

7. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

8. Shell Programming and Scripting

to save output of a command in hash variable

Hi all, is it possible to save the output of a unix command executed in perl to be saved in hash variable.. like i have the command `find $mypath ! -user mainuser -printf \"\%u \%h\\n\"`; this will print all the users other than mainuser with their paths. so is possible to capture... (2 Replies)
Discussion started by: saapa
2 Replies

9. Shell Programming and Scripting

Perl: Pattern Matching a HASH variable

Hi All, I'm trying to test a Hash variable but it's not working. Here is my code - can anyone tell me if the test is valid? for (keys %enabled_yn) { if ($enabled_yn{$1} =~ m/\s+Y/) { $html =~ s/%(\w+)%/\<b\>\<font color\=orange\>$enabled_yn{$1}\<\/font\>\<\/b\>/g; }... (1 Reply)
Discussion started by: pondlife
1 Replies

10. Shell Programming and Scripting

How to create md5 Hash variable?

I have a script that runs the grub-md5-crypt command based on whether the pass_value variable is a non-zero string. The md5 hash is being created in the /opt/hostconfigs/$HOST file, but I can't echo $md5_value. It is blank. Is there a way to create and echo a md5 hash variable? if then... (1 Reply)
Discussion started by: cstovall
1 Replies

Featured Tech Videos