1833876 Members
1753 Online
110063 Solutions
New Discussion

Script help......

 
SOLVED
Go to solution
phil cook
Frequent Advisor

Script help......

Hi all

Sorry to appear lazy but I'd like to write a small script & would appreciate some quick tips as to how I should go about it.

I need a script that can be run on the command line with one text argument. The script will then need to append that argument onto the end of a specific line within a text file.

My problem is that I want admin users to be able to add users onto a group definition statement within the sudo config file /etc/sudoers. I'll have to make a copy firts & perform the append before copying it back over the actual file, this bit I think I can manage.

Any ideas as to the simplest way of doing this would be great.

Thanks

Phil C
Do I have to?
10 REPLIES 10
Praveen Bezawada
Respected Contributor
Solution

Re: Script help......

Hi

You can the below lines in the script a.sh

#! /sbin/sh

awk '{ if ( NR == "'$2'" ) { print $0"'$1'" }
else { print $0 } }' filename

run the script as

./a.sh text linenumber

this will append the text in the linenumber

..PRB...

Jack Werner
Frequent Advisor

Re: Script help......

Look at man for sed. It can add text to the end of line(s).
Not necessarily the "quick and dirty" method, but sed is quite useful. It allows you to do "vi" functions to a file in a single pass. You can redirect stdout to experiment and play it safe.
i'm retired
James R. Ferguson
Acclaimed Contributor

Re: Script help......

Hi Phil:

Consider this:

An input file called "myfile" like:

this is line1
this is line2
this is line3

...where you want to append your token to the end of "line2":

Then your script called "my.sh":

#!/usr/bin/sh
sed "s/line2/line2$1/" $2 > ${2).new
mv ${2}.new ${2}

To add the text string "added" to the "line2" token:

./my.sh added myfile

...JRF...
phil cook
Frequent Advisor

Re: Script help......

So it seems there are at least a couple of ways to do it. Can you tell me this, if I'm not certain the line number will remain the same, can I use the sed method but find instead of explicitly refering to the line in question - I know of unique fields that allow me to grep for that line.

Phil
Do I have to?
Sachin Patel
Honored Contributor

Re: Script help......

Hi Phil,
There must be easy way but I know long way to do it in perl

#add_user username (this is how you run)

#/usr/contrib/bin/perl
open (FILE "/etc/sudoers") || die "can't open sudo file";
open (NEWFILE "/etc/newsudoers");
$uername=shift;

while
{
if (/User_Alias OPERATOR/) #if you want to add user_alias
{
$newline=$_.","$username
print NEWFILE $newline;
}
print NEWFILE $_;
}
mv /etc/newsudoers /etc/sudoers;

Sachin

Is photography a hobby or another way to spend $
Sachin Patel
Honored Contributor

Re: Script help......

Wooo people are so fast. When I hit reply it has only one answer when I hit submit it has already 4 answer.

Is photography a hobby or another way to spend $
Joseph C. Denman
Honored Contributor

Re: Script help......

Lets say you have a file that has the adduser command. Lets assume your file looks something like this

#test.dat file
line 1
line 2
line 3
.
.
adduser tester1
..
.
.
#end

No lets say you wanted to modify the adduser line. You could easily do this:

var1=`cat test.dat | grep adduser | cut -f1 -d" "`
sed 's/'${var1}'/'${1}'/ test.dat > test.out;mv test.out test.dat

Hope this helps.

...jcd...

If I had only read the instructions first??
Joseph C. Denman
Honored Contributor

Re: Script help......

I tell you what, you ask a scripting question, we jump all over it. I brought up the reply screen, walked away for a sec and typed in my respone. I was 7th!!!! ha ha

Anyway, in my reponse, it is field 2 not field 1



var1=`cat test.dat | grep adduser | cut -f2 -d" "`
sed 's/'${var1}'/'${1}'/ test.dat > test.out;mv test.out test.dat

...jcd...
If I had only read the instructions first??
Joseph C. Denman
Honored Contributor

Re: Script help......

Another geewiz

grep -n adduser test.dat | cut -f1 -d":"

Will give you the line number.

...jcd...
If I had only read the instructions first??
phil cook
Frequent Advisor

Re: Script help......

Thanks everyone for your advice - now I know where to come!!

I've ended up using the following & it works just fine:

#!/usr/bin/sh
line=`grep 'User_Alias HELPDESK=' /etc/sudoers | cut -f1 -d":"`
sed "s/$line/$line,$1/" /etc/sudoers > /etc/tmpsudoers.new
mv /etc/tmpsudoers.new /etc/sudoers

Thanks again

Phil C
Do I have to?