sed adding new comment lines in crontab - linux

I have a script that contains the following line:
crontab -l |sed -e 's=\(^.*/usr/local/nextone/bin/setdbrole.sh$\)=#\1=' | crontab -
which will add a '#' to a line in the crontab each time it is executed.
But everytime that line is executed comment lines are added to the crontab like the below:
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.2pG8HV installed on Tue May 12 13:02:16 2015)
so my crontab after several execution of that script get filled with hundred of comment lines.
how can I make it stop ?

Depending on your version of crontab, those lines get added at the beginning of the output of crontab -l. Ask sed to remove the two first lines then:
crontab -l | sed -e '1,2d;s=\(^.*/usr/local/nextone/bin/setdbrole.sh$\)=#\1=' | crontab -

Related

How to edit curl output in place?

I am curling commands for a bash script to run an installation on my linux system and then piping it to the bash console.
curl -G website/bashscript.sh | sudo bash
However the commands I receive from curl has an added
echo "Rebooting"
reboot
at the end.
This curl and sudo command is within my own bash script I am writing for automating an installation process.
How do I remove the echo and reboot command from this text and pipe it to bash? I would rather not save it to a file, edit it, then pass it to bash since I really don't want to remove the text file afterwards.
Thanks!
Remove the last line, if it is the word reboot:
sed '${/^reboot$/d}'
The sed syntax: $ selects the last line. { starts a block. /^reboot$/ selects lines, which contain only the word reboot (^ matches the beginning and $ matches the end of the line). d deletes the line in case of a match. } closes the block.
Remove the last line, if it is the phrase echo "Rebooting":
sed '${/^echo "Rebooting"$/d}'
All together:
curl -G website/bashscript.sh |
sed '${/^reboot$/d}' |
sed '${/^echo "Rebooting"$/d}' |
sudo bash
Another variant with grep, which removes the unwanted commands in any line:
curl … | grep -vx 'echo "Rebooting"
reboot' | …

How to delete multiple history entries from ~/.bash_history using scripts?

I wanted to delete commands that are common (such as pwd, ls -l, cd, ) and keep the ones that I might forget how to use. To do this I ran the following commands
history | grep "cd$" | cut -c 1-5 > ~/sandboxArea/histDelTemp.txt
cntr=0; for i in $(cat ~/sandboxArea/histDelTemp.txt); do var=`expr $i - $cntr`; history -d $var; cntr=`expr $cntr + 1`; done; unset cntr
After running the above command if I again run to check if everything was deleted using the following command
history | grep "cd$"
then it seems that everything is in fact deleted and the above command produces no output.
However, if I close the terminal and again run the above command then everything seems to re-appear. Why is it so? Does bash has functionality to backup_restore commands?
Thanks in advance

How to grep lines in a cron job whose crontab appears to be deleted?

I'm trying to search for a specific word in my cron job, which we'll call word for now.
The machine is an AWS server, and I've only ever used one username, so I do not think it is an issue of cron jobs being under a different user.
So, in the root directory, I do egrep -r ".*word.*" . Nothing comes up. I would assume the original crontab was deleted at some point, even though the process is still running.
However, when I do crontab -l, I comb through the entire output (takes a while), I can see that there definitely is a cronjob with this word.
What is the best way to grep lines in a cronjob? Or, does egrep only work on certain types of files? Thanks.
Wait. If you're doing this:
egrep -r ".*word.*"
It won't return because you haven't pointed egrep at a file to grep through. Generally, you use grep like this:
egrep "word" filename
egrep -r "word" directory/
egrep -r "word" *
You can grep the output of crontab -l like this:
crontab -l | egrep "word"
Other notes:
the .*word.* is unnecessary, as word will match. It should be faster (greedy matching)
egrep -r word * in your home directory wouldn't match the crontab, as that isn't stored in your homedir. (it's likely at /var/spool/cron/crontabs/$USER, but that's not terribly relevant)
you simply have to do:
crontab -l | grep 'word'
That's all.

Simple command for showing comments on shell script

Suppose you have the following script:
# My comment line 1
# My comment line 2
# My comment line 3
echo "My script"
cd $MY_PATH
./anotherScript.ksh
Is there any command to show:
# My comment line 1
# My comment line 2
# My comment line 3
Alternatively, I could write a script to detect the first block of comments.
Thanks
Try this:
grep '^\#.*$' myscript.sh
You could cat script.sh | grep ^\# to only show those lines.
This will output your file to the grep command which will print each line with a "#"
echo "My script" | grep "#"
EDIT I'M DUMB
all you need to do is grep '#' file if I'm understanding correctly this time.

using Linux shell script to edit and rename a file

I am trying to execute a command using Vi or ex to edit a file by deleting the first five lines, replace x with y, remove extra spaces at the end of each line but retain the carraige returns, and remove the last eight lines of the file, then rename the file into a shell script and run the new script from the current script.
This will be something that is scheduled in cron. I have been looking for a simple way to do it using the command line or a Vim script or something.
Any ideas? The format of the input file does not change, just the amount of lines, so I can't specify the line numbers for the last eight lines.
You actually have about half a dozen questions here. Here's an answer for the first five which are probably the ones you'll have the most difficulty solving:
sed -e ':label' -n -e '1d' -e 's/x/y/g' -e 's/[ \t]*$//g' -e '1,9!{P;N;D};N;b label' file.txt > script.sh
Vi is an interactive editor. You probably don't want to use it for something that'll be run by cron. Also, I agree with the comments saying this is probably a bad idea. Be that as it may:
printf 'one\ntwo\nthree\nfour\nfive\necho x \n1\n2\n3\n4\n5\n6\n7\n8\n' \
| sed '1,5d;s/ *$//;s/x/y/' \
| tail -r | sed 1,8d | tail -r \
| sh
Our first sed script does most of the work. We reverse the lines with tail -r, then delete the first 8 lines, then reverse again. That trims off the last 8 lines.
Note that on Linux systems (or any with GNU coreutils), you may also have a tac command which reverse lines, but tail -r is more portable.
Also, the final | sh simply runs the output. If you REALLY want to save this as a script, you can do that by redirecting the output to a file ... but I'll leave at least that to your imagination. Can't do all your scripting for you, can we?! :-)
To edit a file by a script, you could use ed (even if it hard to learn or remember).
You could also use some scripting language (Python, Perl, AWK, Ruby) to achieve your goal.

Resources