Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to escape quotes in shell function?

Tags:

raku

This is Rakudo Star version 2019.03.1 built on MoarVM version 2019.03 implementing Perl 6.d.

Windows 10

Examples:

1) Error:

shell 'mysqldump -uroot -ppassword asppmr > D:\b\29-09-2019 19-45-18\asppmr.sql';

mysqldump: [Warning] Using a password on the command line interface can be insecure. mysqldump: Couldn't find table: "19-45-18\asppmr.sql" Proc.new(in => IO::Pipe, out => IO::Pipe, err => IO::Pipe, exitcode => 6, signal => 0, pid => 11928, command => ("mysqldump -uroot -ppassword asppmr > D:\b\29-09-2019 19-45-18\asppmr.sql",))

2) Error:

shell 'mysqldump -uroot -ppassword asppmr > "D:\b\29-09-2019 19-45-18\asppmr.sql"';

Синтаксическая ошибка в имени файла, имени папки или метке тома. Proc.new(in => IO::Pipe, out => IO::Pipe, err => IO::Pipe, exitcode => 1, signal => 0, pid => 19372, command => ("mysqldump -uroot -ppassword asppmr > \"D:\b\29-09-2019 19-45-18\asppmr.sql\"",))

3) No error (when there are no spaces in the file path):

so shell 'mysqldump -uroot -ppassword asppmr > D:\b\asppmr.sql';

True

4) cmd.exe no error:

mysqldump -uroot -ppassword asppmr > "D:\b\29-09-2019 19-45-18\asppmr.sql"

5) perl 6 no error:

my $r = q:x/mysqldump -uroot -ppassword asppmr/;
spurt('D:\b\27-09-2019 18-29-12\asppmr.sql', $r);

6) perl 6 no error (if there are no quotes in the file path):

print 'mysql dump: ';
my $d = run 'C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump',
    '-uroot',
    '-ppassword',
    'asppmr',
    '--result-file=D:\b\29-09-2019 19-45-18\asppmr.sql',
    :err;

$d.err.slurp(:close); # skip show errors
say $d.exitcode == 0 ?? 'success!' !! 'error!';

mysql dump: success!

Solution: (thanks r4ch)

my $fpath = 'D:\b\29-09-2019 19-45-18\asppmr.sql';
$fpath.subst-mutate(' ', '^ ', :g) if $*DISTRO.is-win;
shell "mysqldump -uroot -ppassword asppmr > {$fpath}";
like image 993
Shniperson Avatar asked Sep 30 '19 07:09

Shniperson


People also ask

How do you escape a quote in a shell script?

Escape with a backslash every double quote character and every backslash character: " ==> \", \ ==> \\

How do you escape quotes?

You can put a backslash character followed by a quote ( \" or \' ). This is called an escape sequence and Python will remove the backslash, and put just the quote in the string.

How do you escape double quotes in shell?

Single quotes(') and backslash(\) are used to escape double quotes in bash shell script. We all know that inside single quotes, all special characters are ignored by the shell, so you can use double quotes inside it. You can also use a backslash to escape double quotes.

How do you escape and in shell?

The escape (\) preceding a character tells the shell to interpret that character literally. With certain commands and utilities, such as echo and sed, escaping a character may have the opposite effect - it can toggle on a special meaning for that character.


1 Answers

You can try escaping the space in the file path with ^ as suggested in How to escape space in file path in windows run?

like image 118
gnvk Avatar answered Nov 15 '22 09:11

gnvk