With given file do following: a.Do allmodifications in this file, using bash,this is the…
- User
With given file do following: a.Do allmodifications in this file, using bash,this is the content:
rtkit:111:RealtimeKit,,,:/usr/sbin/nologin:::/bin/zsh dnsmasq:112:dnsmasq,,,:/usr/sbin/nologin:::/bin/zsh cups-pk-helper:113:user for cups-pk-helper service,,,:/usr/sbin/nologin:::/bin/zsh speech-dispatcher:114:Speech Dispatcher,,,:/bin/false:::/bin/zsh kernoops:116:Kernel Oops Tracking Daemon,,,:/usr/sbin/nologin:::/bin/zsh saned:117::/bin/bash:::/bin/zsh nm-openvpn:118:NetworkManager OpenVPN,,,:/usr/sbin/nologin:::/bin/zsh hplip:119:HPLIP system user,,,:/bin/false:::/bin/zsh whoopsie:120::/bin/false:::/bin/zsh geoclue:122::/usr/sbin/nologin:::/bin/zsh gnome-initial-setup:124::/bin/false:::/bin/zsh gdm:125:Gnome Display Manager:/bin/false:::/bin/zsh ac:1000:ac,,,:/usr/bin/zsh:::/bin/zsh systemd-coredump:999:systemd Core Dumper:/usr/sbin/nologin:::/bin/zsh _rpc:126::/usr/sbin/nologin:::/bin/zsh statd:127::/usr/sbin/nologin:::/bin/zsh vde2-net:128::/usr/sbin/nologin:::/bin/zsh stunnel4:129::/usr/sbin/nologin:::/bin/zsh
b. Change shell for user saned from /usr/sbin/nologin to /bin/bash using AWK c. Change shell for user avahi from /usr/sbin/nologin to /bin/bash using SED d. Save only 1-st 3-th 5-th 7-th columns of each string based on : delimiter e. Remove all lines from file containing word daemon f. Change shell for all users with even UID to /bin/zsh g. passwd_new should not have a new line at the end of the file
Make sure you are not using any additional installed packages (like gawk and etc.)
- Assistant
Here is a solution in Bash that accomplishes all the tasks listed for the given file contents. This solution will use standard Unix command-line tools such as
awk,sed,grep, andcutto achieve the objectives:#!/bin/bash # Save the given content to a file called passwd cat <<EOF > passwd rtkit:111:RealtimeKit,,,:/usr/sbin/nologin:::/bin/zsh dnsmasq:112:dnsmasq,,,:/usr/sbin/nologin:::/bin/zsh cups-pk-helper:113:user for cups-pk-helper service,,,:/usr/sbin/nologin:::/bin/zsh speech-dispatcher:114:Speech Dispatcher,,,:/bin/false:::/bin/zsh kernoops:116:Kernel Oops Tracking Daemon,,,:/usr/sbin/nologin:::/bin/zsh saned:117::/bin/bash:::/bin/zsh nm-openvpn:118:NetworkManager OpenVPN,,,:/usr/sbin/nologin:::/bin/zsh hplip:119:HPLIP system user,,,:/bin/false:::/bin/zsh whoopsie:120::/bin/false:::/bin/zsh geoclue:122::/usr/sbin/nologin:::/bin/zsh gnome-initial-setup:124::/bin/false:::/bin/zsh gdm:125:Gnome Display Manager:/bin/false:::/bin/zsh ac:1000:ac,,,:/usr/bin/zsh:::/bin/zsh systemd-coredump:999:systemd Core Dumper:/usr/sbin/nologin:::/bin/zsh _rpc:126::/usr/sbin/nologin:::/bin/zsh statd:127::/usr/sbin/nologin:::/bin/zsh vde2-net:128::/usr/sbin/nologin:::/bin/zsh stunnel4:129::/usr/sbin/nologin:::/bin/zsh EOF # b. Change shell for user saned from /usr/sbin/nologin to /bin/bash using AWK awk -F: '/^saned:/ {$6="/bin/bash"}1' OFS=: passwd > passwd_temp && mv passwd_temp passwd # c. Change shell for user avahi from /usr/sbin/nologin to /bin/bash using SED sed -i '/^avahi:/s|/usr/sbin/nologin|/bin/bash|' passwd # d. Save only 1st, 3rd, 5th, 7th columns of each string based on : delimiter awk -F: '{print $1 ":" $3 ":" $5 ":" $7}' passwd > passwd_temp && mv passwd_temp passwd # e. Remove all lines from file containing word daemon grep -vi daemon passwd > passwd_temp && mv passwd_temp passwd # f. Change shell for all users with even UID to /bin/zsh awk -F: '($2 % 2 == 0) {$4="/bin/zsh"}1' OFS=: passwd > passwd_temp && mv passwd_temp passwd # g. Make sure the file does not have a newline at the end truncate -s -1 passwd # Rename the final file to passwd_new mv passwd passwd_newExplanation:
- Creates a file called
passwdwith the given content. - Task b: Uses
awkto change the shell for the usersanedfrom/usr/sbin/nologinto/bin/bash. - Task c: Uses
sedto change the shell for the useravahifrom/usr/sbin/nologinto/bin/bash. - Task d: Uses
awkto print only the 1st, 3rd, 5th, and 7th columns of each line. - Task e: Uses
grepto remove all lines that contain the word "daemon". - Task f: Uses
awkto change the shell for users with even UID to/bin/zsh. - Task g: Uses
truncateto remove the last new line character from the file. - Renames the modified file to
passwd_new.
- Creates a file called