Re: Strange system call failure in OS X
by moritz (Cardinal) on Feb 15, 2013 at 20:26 UTC
|
Right now you're assigning a single string to the array @args. Instead you can assign a list to it, and omit the quoting entirely:
@args = (qw(scutil --set ComputerName), $name);
system(@args)
Though I guess that the problem is not with quoting, but something else.
Perhaps I should insert some sort of sleep command to pause between system calls?
And what would you gain from that?
| [reply] [d/l] [select] |
|
# Disable autologin
@args = ("defaults delete /Library/Preferences/com.apple.loginwindow a
+utoLoginUser");
system(@args);
# exit our test version here before doing anything irrevocable
exit 0;
# Serialize Adobe CS6 Design and Web Premium
@args = ("/Library/AdminToolBox/AdobeSerialization adobe_prtk --tool=V
+olumeSerialize --provfile=/Library/AdminToolBox/prov.xml");
system(@args);
# Bind to Active Directory
@args = ("/Library/AdminToolBox/bind.sh");
system(@args);
# This time we'll use exec since we are restarting anyway
@args = ("/sbin/shutdown -r now");
exec(@args);
| [reply] [d/l] |
|
Do I break it into a separate item for each argument?
Yes, it is system "/usr/bin/foobar", "arg", "arg", "--arg=arg" ...
Any need for quotes?
No. Quotes are for the shell.
See system/exec.., kinda dense/long, but essentially system q{single "st" "ri" "ng"} could be open to shell interpolation/invoke the shell, while system "single", "st", "ri", "ng" will avoid the shell
So list is preferable because you don't have to quote to a particular shell , just quote for perl
perlop#Quote and Quote like Operators discusses making a list with qw" st ri ng ";
While you're in a learning mood, get a free copy of Modern Perl
See also
| [reply] [d/l] [select] |
|
|
Sending a list to system is much cleaner, thank you, but the error remains. I think perhaps for now I will just delete the system call that isn't working, as when I inserted a call to get that preference, the correct value came back -- so perhaps I don't really need to set that particular value. (I read about this command on osxdaily.com.) Regarding sleep, there was nothing to be gained. I tried a sleep 4 without any change in the error. Perhaps it is a newbie attribute to hope that inserting a sleep will make a problem go away. <grin>
| [reply] |
Re: Strange system call failure in OS X
by Corion (Patriarch) on Feb 15, 2013 at 20:27 UTC
|
For debugging, have you tried hardcoding a valuee instead of using $name? If you think a delay is necessary, look at sleep. | [reply] |
Re: Strange system call failure in OS X
by kcott (Archbishop) on Feb 16, 2013 at 07:50 UTC
|
G'day endor-moon,
Welcome to the monastery.
I have Mac OSX Lion, so check your (Mountain Lion) system for any relevant differences to what I've posted below.
The error message says the argument is Invalid (i.e. not Empty, Missing, etc.). I'd say "... third gets an empty string." is an unreasonable assumption.
Running man scutil, I see --set pref [newval] with the following listed (under the --get option):
Supported preferences include:
ComputerName The user-friendly name for the system.
LocalHostName The local (Bonjour) host name.
HostName The name associated with hostname(1) and gethos
+tname(3).
Given this, I'd read "Invalid argument" as "Invalid local (Bonjour) host name".
I know nothing about Bonjour but an internet search for "mac osx bonjour" produced many results including a number of hints/tips/tricks/howto type pages. https://www.apple.com/support/bonjour/ led me to Bonjour Overview: Domain Naming Conventions which contains a number of Bonjour Names for ... sections. I'll leave you to continue researching from there.
| [reply] [d/l] [select] |
Re: Strange system call failure in OS X
by Anonymous Monk on Feb 15, 2013 at 20:26 UTC
|
| [reply] |
|
Thanks, I tried printing the value before sending. I will also use chomp in case there is a newline character there. Hah, whaddyknow, chomp solved the problem. Thanks for the debugging link.
| [reply] |
Re: Strange system call failure in OS X
by karlgoethebier (Abbot) on Feb 16, 2013 at 13:38 UTC
|
BTW, why don't you do it like this:
#!/usr/bin/env perl
use strict;
use warnings;
my $name = qq(3c0754131c6d);
my $result;
eval { $result = qx(scutil --set LocalHostName $name) };
..and then take a look $result, $?>>8 and $@?
Just an idea, don't know if this helps but best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] [select] |
|
# Declare dependencies
use File::Slurp;
# Declare variables we'll be using
my $name = "";
my @args = ();
my $result = "";
# Set computer name based on MAC address of en0
my @entries = qx(ifconfig en0 | awk '/ether/ { gsub(":", ""); print \$
+2 }');
chomp(@entries);
$name = $entries[0];
| [reply] [d/l] |
|
Hi endor-moon and welcome,
After bothering myself a bit with awk (i repressed the details) i decided to take the swiss katana. I'm assuming that you just want the mac address:
#!/usr/bin/env perl
+
use strict;
use warnings;
my $mac = ( split " ", ( qx(ifconfig en0) )[2] )[1];
print qq($mac\n);
__END__
Karls-Mac-mini:monks karl$ ./endor-moon.pl
2c:07:54:5e:3e:f0 # i faked this, really ;-)
This works for me. Please note also that your code didn't output anything (for me).
Btw, why do you load a module that you don't use (File::Slurp) as well as some variables (@args, $result)...or do i miss something?
My best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] [select] |
|
eval { $result = qx(scutil --set LocalHostName $name) };
- qx// is the generic form of `...`
- `...` passes a single string to the shell (except when perl is sure that a shell is not needed), so you have to handle an unknown shell with unknown behavior. Quoting is necessary to prevent shell injection, but quoting rules differ from shell to shell. See http://www.in-ulm.de/~mascheck/various/ to get an idea how different shells can behave.
- `...` does not die, eval won't catch any error in external programs, so eval is useless here.
Read the "Safe Pipe Opens" section of perlipc for a secure replacement for `...`.
The shell problem is also present in the single argument versions of system and exec, so make sure to use the multi-argument versions, which don't have this problem.
Alexander
--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
| [reply] [d/l] [select] |
|
#!/usr/bin/env perl
+
eval { qx(nose --cuke) || die qq(shit: $!\n) };
print qq(Trapped this $@) if $@;
__END__
Karls-Mac-mini:monks karl$ ./nose.pl
Trapped this shit: No such file or directory
Update: I always thought that it isn't a bad idea to wrap a call to a subshell into a eval block. And i don't know what is generic with qx. It's just the same as backticks, isn't it? Just a subshell.
Regards, Karl
«The Crux of the Biscuit is the Apostrophe»
| [reply] [d/l] [select] |