#How to print remainder of line using awk
eliminate the first X columns…
Here is your challenge you have a list of cronjobs and you want to just execute the scheduled backup command with its arguments for a test run.
Using one command line, how would you do it?
Here is a sample root cronjob and I want to run the first entry with its arguments as a test run:
# m h dom mon dow command
0 16 * * * /data/share/home/geoffm/dev/utils/bak-it.sh -i /usr/local/etc/include.lst -e /usr/local/etc/exclude.lst -t /data/bak/`hostname` -s 7 >/dev/null 2>&1
5 0 * * * /data/share/motion/arch-files.sh >/dev/null 2>&1
*/15 * * * * /data/share/home/geoffm/grab-pic.sh >/dev/null 2>&1
####### below are WIP or OLD ########
# 16 6 * * * /usr/sbin/lynis -c --auditor "automated" --cronjob --quiet >/dev/null 2>&1
#*/5 * * * * /usr/local/bin/watch-proc.sh -f /usr/local/etc/watch-proc.conf -n geoffm@localhost >/dev/null 2>&1
#*/5 * * * * /data/share/home/geoffm/ping-pong.sh "ping -c2 192.168.1.55" "ssh 192.168.1.1 reboot" >/dev/null
# 7 * * * /usr/local/sbin/postfix_report.sh &> /dev/null
Here is how I would do it:
sudo $(sudo crontab -l | awk '/bak-it/{$1=$2=$3=$4=$5="";print $0}')
Examining this we first see this $(...)
structure which tells
the shell to run whatever exists between the parens and replace the
output right there. So first we call sudo crontab -l in order to
make sure we get root’s crontab listed. The output is piped into
awk where we give it a regex to search for in each string (ie /bak-it/).
The trick here is we tell awk to make the first variable in the string
equal to the second… equal to the 3rd, 4th and 5th, and finally = “”.
This effectively substitutes and empty string for the first 5 variables
leaving the remained of the line which then gets printed with the
print $0
. Actually you can leave off the $0
because awk will default
to that if you just use print
alone.
Now there may be one more problem. If you run that command above it will execute the intended command along with the arguments BUT it will do it as the user who ran it. Obviously, if the commands come from the root crontab it may not run properly for another user.
I will leave it as an exercise for the reader to solve that problem.
comments powered by Disqus