Blank space cause error when use perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Blank space cause error when use perl
# 1  
Old 04-05-2009
Blank space cause error when use perl

I write a script with register and login user.
So, i encrypt password with
encryptedpass=`perl -e "print crypt("${mypass}",salt)"`

if password do not contain blank space, it work
but if password have blank space, it cause error in that line

the error is:
syntax error at -e .....

Anyone have solution? Or face this before, how to solve this?
Thank you.

Last edited by WuZun; 04-05-2009 at 11:09 AM..
# 2  
Old 04-05-2009
It's the quoting. If you run that part with tracing enabled (set -x), it will look something like this:
Code:
> set -x
> export mypass=1234
+ export mypass=1234
+ mypass=1234
> encryptedpass=`perl -e "print crypt("${mypass}",salt)"`
++ perl -e 'print crypt(1234,salt)'
+ encryptedpass=saS3eimg8Mg1M
> export mypass="1234 5678"
+ export 'mypass=1234 5678'
+ mypass='1234 5678'
> encryptedpass=`perl -e "print crypt("${mypass}",salt)"`
++ perl -e 'print crypt(1234' '5678,salt)'
syntax error at -e line 1, at EOF
Execution of -e aborted due to compilation errors.
+ encryptedpass=
>

As you can see, in the second example Perl gets passed 3 options (-e, 'print crypt(1234', and '5678,salt)' ) which in this case in invalid. One of those changes should work:
Code:
encryptedpass=`perl -e "print crypt(\"${mypass}\",salt)"`
encryptedpass=`perl -e "print crypt('${mypass}',salt)"`

The first has variable substitution enabled for both the shell and Perl, but is a bit less readable. The second disables substitution within Perl, but has better readability. In the end it's a question of style.
# 3  
Old 04-05-2009
It worked after i made change follow your post
Thank you so much

in my case, i can't use
Code:
'${mypass}'

because it will understand ${mypass} as a string not a variable
so the first is better (in my case).

Thank you again. Smilie
# 4  
Old 04-05-2009
Quote:
Originally Posted by WuZun
in my case, i can't use
Code:
'${mypass}'

Yes, you can, because the outermost quotes are relevant to the shell in this case. Example:
Code:
> set -x
> export mypass=1234
+ export mypass=1234
+ mypass=1234
> encryptedpass=`perl -e "print crypt(\"${mypass}\",salt)"`
++ perl -e 'print crypt("1234",salt)'
+ encryptedpass=saS3eimg8Mg1M
> encryptedpass=`perl -e "print crypt('${mypass}',salt)"`
++ perl -e 'print crypt('\''1234'\'',salt)'
+ encryptedpass=saS3eimg8Mg1M
>

The only difference are the quotes passed to Perl, at which point the variable is already substituted. It would only make a difference with variables that you defined within the Perl command that the shell has no knowledge of.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to identify blank space in a file

Hello, I have a dictionary of over 400,000 words with the following structure source=target The database contains single words as well as phrases. To train the data, I need only mappings with out a space i.e. where both source and target do not have any space in between. I use Ultraedit as... (4 Replies)
Discussion started by: gimley
4 Replies

2. Shell Programming and Scripting

Removing blank space using VI

Hi, How to remove blank spaces using vi (I am using AIX)? #cat siva.txt AAA BBB CCC DDD EEE FFF Need to remove space between 2 columns. Regards, Siva (7 Replies)
Discussion started by: ksgnathan
7 Replies

3. Shell Programming and Scripting

Not delete space blank

Hi everyone, i need to "grep" a file with a string with space blanks, like this: grep "XXXX XX" file.txt The problem, i need put the "XXXX XX" in a string variable. When the script executes the grep, do: gresp XXXX XX file.txt How can i solve this problem? The... (5 Replies)
Discussion started by: Xedrox
5 Replies

4. Shell Programming and Scripting

Want non-interpretation of blank space using cat

I have a file say ADCD which is like following--> Please consider 'z' as space #cat ADCD <!--Yzzz|z--> <!--Nzzzzz--> Now I want to store the content of this file to a variable say VAR like this--> #VAR=`cat ADCD` #echo $VAR <!--Yz|z--> <!--Nz--> Now I don' t want the variable... (2 Replies)
Discussion started by: muchyog
2 Replies

5. Shell Programming and Scripting

Removing blank space in file

TT0000013101257 | JCJMMUJMMUB018 ... (0 Replies)
Discussion started by: sususa
0 Replies

6. UNIX for Dummies Questions & Answers

Replace colon with blank space

Dear Gurus, I have a unix file with multiple colons on each row, and I would like to replace each colon with a blank space using the awk command. For example, I have the following data: Data: --------- A~000000000000518000~SLP:~99991231~20090701~00102.00~USD:~CS:~... (2 Replies)
Discussion started by: chumsky
2 Replies

7. UNIX for Dummies Questions & Answers

blank space

hi everyone, i have a problem in unix script , i need to remove line that has blank , not blank line . example: mahm,,jdggkhsd,ghskj,,fshjkl can anyone help? (4 Replies)
Discussion started by: Reham.Donia
4 Replies

8. Shell Programming and Scripting

String starting with blank space

Dear Masters, Need your help for this small query. I am trying to get all the strings that starts with a blank space as output. Limitation is , it should be done only through AWK. Input will be: aa _1bbb c-ccc ddd eeeee ff Output should be: _1bbb c-ccc eeee ff (5 Replies)
Discussion started by: patric2326
5 Replies

9. Shell Programming and Scripting

Cut last blank space

Hello, I am using this to get only directories : ls -l | grep '^d'and here is the result : drwx------ 13 so_nic sonic 13 Nov 4 13:03 GLARY drwx------ 3 so_nic sonic 3 May 6 2010 PSY2R drwx------ 15 so_nic sonic 15 Oct 14 08:47 PSYR1 But I only need to keep this... (7 Replies)
Discussion started by: Aswex
7 Replies

10. Shell Programming and Scripting

append blank space

Hi, I would like to add blank space for fixed length(50) if length of string <30. Scenario: File Size AAA.CSV 123 BB.CSV 134 Expected: File Size AAA.CSV 123 BB.CSV 134 I want append blank space until 30 character. Thanks and Regards, HAA (1 Reply)
Discussion started by: HAA
1 Replies
Login or Register to Ask a Question