in reply to Open Filehandle once
Hello Dunkellbusch,
Welcome to the Monastery. The monks have already provided you with solutions to your problem, but I would like to add one more. You can use the module File::Slurp/append_file, so you can minimize your code to simply one call in every time you need it, no closing file handles etc.
But to be honest I would combine the module with the nice solution of fellow monk Corion, executable sample bellow below (change grep from /enable/ to /smb\.service/):
#!/usr/bin/perl use strict; use warnings; use File::Slurp; my $filePath = 'test_1.txt'; my @services = `systemctl list-unit-files`; if( my @smb = grep { /enabled/ } @services ) { map { s/\s+\z// } @smb; # remover white space append_file( $filePath, "chkconfig smb: OK\n"); append_file( $filePath, map { "$_\n" } @smb); # append_file( $filePath, "\n"); # if you want to append multiple +times }
Update: Based on comments try to avoid File::Slurp, for simple implementations (read, write, etc) you can use File::Slurper, unfortunately there the author has not added the append proposal. I was looking online and I found the IO::All. Never used it before but I read that it is really really good.
Sample of updated solution, based on commends:
#!/usr/bin/perl use strict; use warnings; use IO::All -utf8; # Turn on utf8 for all io my $path = 'file.txt'; my @services = `systemctl list-unit-files`; if( my @smb = grep { /enabled/ } @services ) { s{ ^\s* | \s*$ }{}gx for @smb; io($path)->appendln(@smb); }
Output:
$ cat test_1.txt chkconfig smb: OK acpid.path enabled cups.path enabled accounts-daemon.service enabled anacron-resume.service enabled anacron.service enabled apparmor.service enabled autovt@.service enabled avahi-daemon.service enabled bluetooth.service enabled console-setup.service enabled cron.service enabled cups-browsed.service enabled cups.service enabled dbus-org.bluez.service enabled dbus-org.freedesktop.Avahi.service enabled dbus-org.freedesktop.ModemManager1.service enabled dbus-org.freedesktop.nm-dispatcher.service enabled dbus-org.freedesktop.resolve1.service enabled dbus-org.freedesktop.thermald.service enabled denyhosts.service enabled display-manager.service enabled dns-clean.service enabled friendly-recovery.service enabled getty@.service enabled gpu-manager.service enabled keyboard-setup.service enabled lightdm.service enabled ModemManager.service enabled network-manager.service enabled networking.service enabled NetworkManager-dispatcher.service enabled NetworkManager-wait-online.service enabled NetworkManager.service enabled ondemand.service enabled openvpn.service enabled pppd-dns.service enabled repowerd.service enabled resolvconf.service enabled rsync.service enabled rsyslog.service enabled setvtrgb.service enabled snapd.autoimport.service enabled snapd.core-fixup.service enabled snapd.service enabled snapd.system-shutdown.service enabled ssh.service enabled sshd.service enabled syslog.service enabled systemd-resolved.service enabled systemd-timesyncd.service enabled thermald.service enabled ufw.service enabled unattended-upgrades.service enabled ureadahead.service enabled vboxautostart-service.service enabled vboxballoonctrl-service.service enabled vboxdrv.service enabled vboxweb-service.service enabled acpid.socket enabled apport-forward.socket enabled avahi-daemon.socket enabled cups.socket enabled snapd.socket enabled uuidd.socket enabled remote-fs.target enabled apt-daily.timer enabled motd-news.timer enabled snapd.refresh.timer enabled
Hope this helps, BR.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Open Filehandle once
by zentara (Cardinal) on Aug 24, 2017 at 17:51 UTC | |
by thanos1983 (Parson) on Aug 24, 2017 at 23:17 UTC | |
|
Re^2: Open Filehandle once
by 1nickt (Canon) on Aug 24, 2017 at 22:00 UTC | |
by kcott (Archbishop) on Aug 25, 2017 at 01:39 UTC | |
by 1nickt (Canon) on Aug 25, 2017 at 16:53 UTC | |
by kcott (Archbishop) on Aug 26, 2017 at 15:57 UTC | |
by Anonymous Monk on Aug 25, 2017 at 02:51 UTC | |
by thanos1983 (Parson) on Aug 24, 2017 at 23:22 UTC | |
|
Re^2: Open Filehandle once (updated)
by soonix (Chancellor) on Aug 25, 2017 at 08:49 UTC |