Dollar symbol in Shell Script Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dollar symbol in Shell Script Variable
# 1  
Old 09-20-2017
Dollar symbol in Shell Script Variable

Hi,

I am working on PGP encryption. I am getting public keys from some file.
One of the key has dollar sign in it "$" Example: "abc$123"

Code:
echo 'passphrase='$passphrase  --> Giving correct value abc$123

But if I use $passphrase in PGP command getting Invalid passphrase error.
If I hardcode "abc$123" encryption is working fine.
Getting error only if I am referring $passphrase in PGP command.

Please help how to handle $ sign in shell script variable

Thanks,
Sreehari

Last edited by Scrutinizer; 09-21-2017 at 02:07 AM.. Reason: code tags
# 2  
Old 09-20-2017
I can't tell why code I can't see isn't working. Please show your code.

I doubt that the dollar sign causes the problem, though. Shell is quite careful not to interpret dollar signs in strings as variables -- only in the shell's code itself is the value special.

Variables inside single quotes don't work, variables inside double quotes are expanded. This might be the issue.

Code:
$ echo '$asdf'

$asdf

$ echo "$asdf"

variablecontents

$


Last edited by Corona688; 09-20-2017 at 07:32 PM..
# 3  
Old 09-20-2017
Single tics or an escape (backslash) stops the shell from translating a variable because it takes the $ literally rather than as a metcharacter.

Code:
password="123\$abc"
# and echo works

echo "$password

Is that what you mean?
# 4  
Old 09-20-2017
Working code: Hardcoded passphrase
Code:
pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase "abc$123" --output $EncryptFile

Not Working:
Code:
echo 'passphrase='$passphrase  --> gives correct value abc$123

pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase $passphrase --output $EncryptFile     ####Gives invalid passphrase

Thanks,
Sreehari

Last edited by Scrutinizer; 09-21-2017 at 02:07 AM.. Reason: code tags
# 5  
Old 09-20-2017
If you don't set passphrase to anything, $passphrase will not work.

If you did set passphrase to something, there may be something wrong with the way you did it. I can't tell because you didn't show that code. Show all your code.

You should probably quote it, too, "$passphrase", to prevent the shell from splitting it on spaces etc.
# 6  
Old 09-20-2017
Hi,

I am maintaining all keys in a file say ABC.pwdf

Contents of ABC.pwdf
================
Code:
<bank> <recipient> <signer> <passphrase>
WFB 0X12343 0x1234 abc$123

================

Reading these keys in shell script
=================
Code:
lnCount=0;
while read pString
do
	lvbank=`echo  ${pString}|cut -d ' ' -f1`

	if [ "$lvbank" = "$l_bank" ]``
	then		
		lnCount=`expr $lnCount + 1`
		recipient=`echo  ${pString} | cut -d ' ' -f2`;
		signer=`echo  ${pString} | cut -d ' ' -f3`;
		passphrase=`echo  ${pString} | cut -d ' ' -f4`;		
	fi;
done < ${PwdFileName};

======================
I am getting all values correctly. Having problem only when I am using passphrase in PGP command

Thanks,
Sreehari

Last edited by Scrutinizer; 09-21-2017 at 02:06 AM.. Reason: code tags
# 7  
Old 09-21-2017
Are you sure you're running pgp with the correct passphrase when using the "hardcoded" one? With what you showed in post#4, wouldn't it look like
Code:
{ read; while read lvbank recipient signer passphrase REST; do echo pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase "abc$123" --output $EncryptFile; done; } < file
pgp -es --Recipient 0X12343 --signer 0x1234 --passphrase abc23 --output

because it expands the $1 positional parameter (which is empty) as opposed to
Code:
{ read; while read lvbank recipient signer passphrase REST; do echo pgp -es $SourceFile --Recipient $recipient  --signer $signer --passphrase $passphrase --output $EncryptFile; done; } < file
pgp -es --Recipient 0X12343 --signer 0x1234 --passphrase abc$123 --output

See the difference?
This User Gave Thanks to RudiC For This Post:
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 can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. Shell Programming and Scripting

Unable to pass shell script variable to awk command in same shell script

I have a shell script (.sh) and I want to pass a parameter value to the awk command but I am getting exception, please assist. diff=$1$2.diff id=$2 new=new_$diff echo "My id is $1" echo "I want to sync for user account $id" ##awk command I am using is as below cat $diff | awk... (2 Replies)
Discussion started by: Ashunayak
2 Replies

3. Shell Programming and Scripting

$ symbol in sql query in shell script

Hi Team, Can you please help me to resolve this issue. Am unable to use this $ symbol in sql query in the shell script. For Example: # !/bin/sh export USER_NAME=XXX export PASSWORD=YYY export ORACLE_SID=xamdb echo $ORACLE_SID echo " Session Details ..." ... (1 Reply)
Discussion started by: indira_s
1 Replies

4. Shell Programming and Scripting

[Solved] cp command with dollar variable in ksh

hi, I have been trying to acheive the following task for a while now, but failed.. Need help, experts please help! This is what I am trying to do: - I am writing to a flat file the name of the source to be copied and the destination path as to where it is to be copied to. Sample flat file:... (7 Replies)
Discussion started by: abdulhusein
7 Replies

5. Shell Programming and Scripting

how to remove the target of the symbol link in a shell script

I have a target directory, there are some files and directories in "target_dir". I have a symbol link: my_link -> <target_dir> The target directory name is NOT known to the script (because it is varying), while the link name is always fixed. In a shell script, how to remove both the... (1 Reply)
Discussion started by: princelinux
1 Replies

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. Shell Programming and Scripting

Expect Script square bracket dollar prompt

Hi Thanks for this amazing forum first, I've been searching answers in it for problems that I've encountered at work. The only problem I haven't been able to find a fix for, is a ever waiting for prompt problem in Expect when encounter a $ prompt. I usually set the timeout to -1 cause the... (2 Replies)
Discussion started by: Ikaro0
2 Replies

8. Shell Programming and Scripting

How to type the Omega symbol Ω in unix shell script

Hi, I have a data file delimited by Ω symbol, I would like to use this in grep and cut command. but How to type this Omega symbol. Thanks Murugesan (1 Reply)
Discussion started by: NagaMurugesan
1 Replies

9. Shell Programming and Scripting

direction symbol in a variable as part of the command

Hi, How can I get this to work? #!/bin/ksh if ; then direction=">>" else direction=">" fi cat some_file_name $direction temp.txt exit This shell script is not happy with using "$direction" opposed to ">" or ">>". Thanks. (5 Replies)
Discussion started by: peterloo
5 Replies

10. Shell Programming and Scripting

Registered Symbol in Korn Shell script

When I am trying to write this symbol in shell script ( ® ) I am getting a . ( dot ) instead , does anybody know how to fix it. Thanks. (1 Reply)
Discussion started by: jacki
1 Replies
Login or Register to Ask a Question