I'm trying to pass a scalar value to the system command but keep having trouble. I'm unsure as to why. Any help would be appreciated at this late hour.
The two errors that I'm getting are:
Syntax Error; Non-Printable Chars for Key.
Error Executing CMD find.
Essentially what I'm trying to pass a string of commands (dbcommand; f; echo;...) within the command "command."
my $id_to_test = $ids[0];
my $cmd = q{command -c "dbcommand -a app-f fam -d db; f sub=a, device=};
$cmd .= q{$id_to_test};
$cmd .= q{, analog=A; echo -c on; echo -o A_value.txt; /DIS;"};
system $cmd;
#system('command-c "dbcommand-a app-f fam-d db; f sub=a, device=$id_to_test, analog=A; echo -c on; echo -o A_value.txt; /DIS;"');
So now I'm doing:
my $id_to_test = $ids[0];
my $cmd = 'command-c \"dbcommand-a app-f fam -d db; f sub=a, device=';
$cmd .= "$id_to_test";
$cmd .= ', analog=A; echo -c on; echo -o A_value.txt; /DIS;\"';
system $cmd;
and I'm getting the error:
- Syntax error; cmd has invalid character(s) -- "dbcommand sh: f: command not found -c on -o A_value.txt sh: /DIS: No such file or directory sh: ": command not found
If I do print $cmd; instead, I also get slashes in front of my double quotes, which is not what I want:
command -c \"dbcommand -a app -f fam -d db; find sub=a, device=1234567, analog=A; echo -c on; echo -o A_value.txt; /DIS;\"
Making sure array is populated:
One of the first things I did was to make sure I was adding values to the array correctly by declaring the array, opening the file, and then doing:
while (<$fh>) {
#Remove any newline characters from line using the chomp() command.
chomp;
push @ids, "$_";
# print($ids[$index]);
# $index = $index + 1;
# print "$row\n";
}
print join (", ", @ids);
my $array_size = @ids;
print("\n" . $array_size);
when I execute the perl script and it prints locally*, everything is as expected -- values are printed and size of array is 3.
123456789, 123456888, 123456789
3
However, when I print remotely, I only get the last element
, 123456789
3
even though the size of the array is also 3.
The problem is when you use the single quote (no interpolation) it's treating your backslash as a literal backslash. The only time I can think of where you need a backslash as an escape character when using a single quote is when you are escaping a single quote itself or a backslash:
Try something like this:
This should also work:
(or you could have wrapped it in one big
""
/q{}
with no concatenation operator).And, as others have hinted,
qq{}
is essentially "", with the added advantage that you can include double quote characters without having to escape them.