How to handling string contains single quote and double quote

V

vikrant

Hi,
I facing a problem in saving a string contains single and double quote
into a file .The string look like the following:-

Example:-
Developer's community "com.lang.perl"
--------------------------------------------------------
The part of Code is:-

#!/bin/perl -w
use strict;

my $Date="10-09-2007";

system("echo '$Date;$String_Value' > data.txt");
----------------------------------------------------------------------------------------
Here,the $String_Value Variable store the string which i have
mentioned above.

I cannot put the slash( \ ) before the double quote's because in my
case value of input string is dynamic.
Also,I know putting a slash before double quote do not solve my
problem because single quote is left which will create the problem.I
get to know about this when i put slash before the double quote's.

The error coming on screen is:-
------------------------------------------------------------------------------------------------
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
-----------------------------------------------------------------------------------------------------

So,please tell me how do i handle this type of input.
I apologize if someone already asked this question earlier.

Thanks and Regards,
Vikrant
 
V

vikrant

Hi,
I facing a problem in saving a string contains single and double quote
into a file .The string look like the following:-

Example:-
Developer's community "com.lang.perl"
--------------------------------------------------------
The part of Code is:-

#!/bin/perl -w
use strict;

my $Date="10-09-2007";

system("echo '$Date;$String_Value' > data.txt");
----------------------------------------------------------------------------------------
Here,the $String_Value Variable store the string which i have
mentioned above.

I cannot put the slash( \ ) before the double quote's because in my
case value of input string is dynamic.
Also,I know putting a slash before double quote do not solve my
problem because single quote is left which will create the problem.I
get to know about this when i put slash before the double quote's.

The error coming on screen is:-
------------------------------------------------------------------------------------------------
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
-----------------------------------------------------------------------------------------------------

So,please tell me how do i handle this type of input.
I apologize if someone already asked this question earlier.

Thanks and Regards,
Vikrant


Sorry for the Subject line mistake it should be something like
following:-
How to handle the string contains single quote and double quote
 
P

Paul Lalli

Hi,
I facing a problem in saving a string contains single and double quote
into a file .The string look like the following:-

Example:-
Developer's community "com.lang.perl"
--------------------------------------------------------
The part of Code is:-

#!/bin/perl -w
use strict;

my $Date="10-09-2007";

system("echo '$Date;$String_Value' > data.txt");
---------------------------------------------------------------------------­-------------
Here,the $String_Value Variable store the string which i have
mentioned above.

I cannot put the slash( \ ) before the double quote's because in my
case value of input string is dynamic.
Also,I know putting a slash before double quote do not solve my
problem because single quote is left which will create the problem.I
get to know about this when i put slash before the double quote's.

The error coming on screen is:-
---------------------------------------------------------------------------­---------------------
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
---------------------------------------------------------------------------­--------------------------

So,please tell me how do i handle this type of input.
I apologize if someone already asked this question earlier.

Why are you calling a system command to do the echoing? Just open the
file for writing in Perl, and print the line to the file in Perl.
Then you don't have to worry about any escaping of quotes.

open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
print $fh "$Date;$String_Value\n";
close $fh;

Paul Lalli
 
V

vikrant

Why are you calling a system command to do the echoing? Just open the
file for writing in Perl, and print the line to the file in Perl.
Then you don't have to worry about any escaping of quotes.

open my $fh, '>', 'data.txt' or die "Cannot open file: $!";
print $fh "$Date;$String_Value\n";
close $fh;

Paul Lalli

Thanks for the information.Actually,the code is a part of a
function,which called again and again.So,i thought that opening and
closing a file on each call may effect the performance.That was the
only reason of using the system command.
Comments are most welcome.

Thanks and Regards,
Vikrant
 
J

Jens Thoms Toerring

Thanks for the information.Actually,the code is a part of a
function,which called again and again.So,i thought that opening and
closing a file on each call may effect the performance.That was the
only reason of using the system command.

Actually, your approach uses a lot more resources and time.
The system() function must create a new process, which in turn
executes a shell to deal with the command you sent to system().
And the file must be opened and closed again anyway, but now
the shell will do that. So all the system() call does is add
overhead, saving you nothing at all (beside making it harder
to check for errors).
Regards, Jens
 
V

vikrant

Actually, your approach uses a lot more resources and time.
The system() function must create a new process, which in turn
executes a shell to deal with the command you sent to system().
And the file must be opened and closed again anyway, but now
the shell will do that. So all the system() call does is add
overhead, saving you nothing at all (beside making it harder
to check for errors).
Regards, Jens

Hi JensThoms,

Thanks for sharing the information.It is very helpful to me.

Regards,
Vikrant
 
J

Joe Smith

vikrant said:
the code is a part of a
function,which called again and again.So,i thought that opening and
closing a file on each call may effect the performance.That was the
only reason of using the system command.
Comments are most welcome.

FYI: Invoking system() like that means 32 separate file operations each
time it is called.

linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars */]) = 0
2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
3 open("/etc/ld.so.cache", O_RDONLY) = 3
4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
7 open("/lib/libdl.so.2", O_RDONLY) = 3
8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
9 open("/lib/libc.so.6", O_RDONLY) = 3
10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
14 open("/etc/mtab", O_RDONLY) = 3
15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
16 open("/proc/meminfo", O_RDONLY) = 3
17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
28 access("/bin/sh", X_OK) = 0
29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
30 access("/bin/sh", X_OK) = 0
31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666
 
W

Wade Ward

Joe Smith said:
vikrant said:
the code is a part of a
function,which called again and again.So,i thought that opening and
closing a file on each call may effect the performance.That was the
only reason of using the system command.
Comments are most welcome.

FYI: Invoking system() like that means 32 separate file operations each
time it is called.

linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars
*/]) = 0
2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file
or directory)
3 open("/etc/ld.so.cache", O_RDONLY) = 3
4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
7 open("/lib/libdl.so.2", O_RDONLY) = 3
8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
9 open("/lib/libc.so.6", O_RDONLY) = 3
10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
14 open("/etc/mtab", O_RDONLY) = 3
15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
16 open("/proc/meminfo", O_RDONLY) = 3
17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) =
0
19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file
or directory)
26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file
or directory)
27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
28 access("/bin/sh", X_OK) = 0
29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
30 access("/bin/sh", X_OK) = 0
31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666
What's the 32nd?
 
V

vikrant

vikrant said:
the code is a part of a
function,which called again and again.So,i thought that opening and
closing a file on each call may effect the performance.That was the
only reason of using the system command.
Comments are most welcome.

FYI: Invoking system() like that means 32 separate file operations each
time it is called.

linux% strace -f -e trace=file sh -c 'echo foo > temp.txt' |& cat -n
1 execve("/bin/sh", ["sh", "-c", "echo foo > temp.txt"], [/* 41 vars */]) = 0
2 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
3 open("/etc/ld.so.cache", O_RDONLY) = 3
4 fstat64(3, {st_mode=S_IFREG|0644, st_size=79328, ...}) = 0
5 open("/lib/libtermcap.so.2", O_RDONLY) = 3
6 fstat64(3, {st_mode=S_IFREG|0755, st_size=12924, ...}) = 0
7 open("/lib/libdl.so.2", O_RDONLY) = 3
8 fstat64(3, {st_mode=S_IFREG|0755, st_size=16244, ...}) = 0
9 open("/lib/libc.so.6", O_RDONLY) = 3
10 fstat64(3, {st_mode=S_IFREG|0755, st_size=1485672, ...}) = 0
11 open("/dev/tty", O_RDWR|O_NONBLOCK|O_LARGEFILE) = 3
12 open("/usr/lib/locale/locale-archive", O_RDONLY|O_LARGEFILE) = 3
13 fstat64(3, {st_mode=S_IFREG|0644, st_size=49641600, ...}) = 0
14 open("/etc/mtab", O_RDONLY) = 3
15 fstat64(3, {st_mode=S_IFREG|0644, st_size=414, ...}) = 0
16 open("/proc/meminfo", O_RDONLY) = 3
17 fstat64(3, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
18 stat64("/home/jms", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
19 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
20 open("/usr/lib/gconv/gconv-modules.cache", O_RDONLY) = 3
21 fstat64(3, {st_mode=S_IFREG|0644, st_size=22294, ...}) = 0
22 open("/usr/lib/gconv/ISO8859-1.so", O_RDONLY) = 3
23 fstat64(3, {st_mode=S_IFREG|0755, st_size=5340, ...}) = 0
24 stat64(".", {st_mode=S_IFDIR|0755, st_size=12288, ...}) = 0
25 stat64("/usr/bin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
26 stat64("/usr/sbin/sh", 0xbff56eb4) = -1 ENOENT (No such file or directory)
27 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
28 access("/bin/sh", X_OK) = 0
29 stat64("/bin/sh", {st_mode=S_IFREG|0755, st_size=686520, ...}) = 0
30 access("/bin/sh", X_OK) = 0
31 open("temp.txt", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666

Once again thanks for providing this valuable information.i shall try
to follow the above procedure to get the behavior of the code.

Regards,
vikrant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top