Bash '||' OR operator not working in Perl?
I'm trying to make a little Perl script to mount/umount devices on the command line.. I came up with the following
but in line 8 the '||' operator is not working (its works by itself on the command line)... BTW the scripts uses a while loop so I can keep toggling mount/umount devices. Code:
while ( <<>> ) { |
Quote:
I do not do perl https://www.tutorialspoint.com/perl/...rs_example.htm but, examine your logical condition if true && true || false if false || do something the condition you're using needs to give back a status of completion or a fail of completion in order to execute any further code within that statement. Code:
is true no echo Code:
[[$(system "udisksctl mount -b /dev/$devices[$index] > /dev/null 2>&1) ]] || umount /dev/$devices[$index]"; therefore it should be written like this. Code:
# if mounted then un-mount && , or if not mounted then mount || this is better sutied for your perl script The ternary operator in Perl |
Thanks for replying, Perl is a glue programming language so it should execute bash code natively (I don't have to do what you're saying in a bash script). But I see what you're saying about surrounding it in brackets to capture the return status and having that factor in on whether the next command should run or not. Ill check to see how to do that in Perl and if that works, thank you..
|
The syntax for the system call is
Code:
system() Note that the default shell for the system() in Unix is sh -c -- not sure what the default is for Linux. Might be bash, but you'd need to check. |
Quote:
Code:
@args = ( "command", "arg1", "arg2", ... ) Code:
$mount_cmd = "udisksctl mount -b /dev/$devices[$index] > /dev/null 2>&1"; HTH... Cheers. |
@scasey, You can omit the parens as long as it doesnt change the meaning of the code.
@rnturn, Thats I good idea I'll try braking it up into variables, and if that doesnt work ill try two system calls thanks.. Im trying to write elegant code.... |
@rnturn thanks it works now though I cant get >/dev/null 2>&1 to work for the same reason... But besides that it all works perfectly..
Code:
This is free software.. Code:
$mount="udisksctl mount -b /dev/$devices[$selected] >/dev/null 2>&1"; |
What is the 'short form'? What "doesn't work" actually means?
|
Quote:
Code:
$mount="udisksctl mount -b /dev/$devices[$selected] >/dev/null 2>&1"; then it will unmount it, but in the process the mount command will return in error.. I want to "hide" that error by redirecting STDERR to /dev/null.. But its not working.. If you notice in my working code above I removed '>/dev/null 2>&1' cause it broke the script.. |
Try this in shell:
Code:
true >/dev/null 2>&1 || echo "true(1) gave error" Code:
false(1) gave error Code:
#!/usr/bin/perl -w Code:
false(1) gave error (It would be less problematic, if false(1) would be called fail(1), and true(1) would be called success(1).) |
So you're saying the nonzero return values are actually true (backwords from the traditional "0 but true" notion that the shell uses). If that's the case than all I should have to do is remove the negation (!) operator, but somehow I don't think that's going to work... Cause i'm pretty sure I tried that already, but i'll try it again.. But rather what if the bash command is giving a nonzero exit status that actually indicates success (as they can return whatever their programmed to), is that a possability? Anyway thanks for bringing that to my attention, I didn't know that until now.
|
well, for a kick-off you are doing the redirections in the wrong order:
Code:
>/dev/null # stdout to null |
you don't use awk etc from Perl,
take a look at what Perl can really do: using the -J switch on lsblk (You may find it interesting, or not) Code:
use strict; |
All times are GMT -5. The time now is 05:30 AM. |