Solved

Nawk - SED problems

Posted on 2008-10-01
6
319 Views
Last Modified: 2013-11-05
Hi all,

Could anyone tell me what's wrong with script below?

echo "Please enter username"
read name
echo "please enter new gid"
read id

sed 's/$name:.*:.*:$id:.*:.*:.*' /etc/passwd > logfile

or

nawk -F: -v user=$"name" '$1 == user{print $1 $4 == "$id}' /etc/passwd

i think i have a syntax error

please help
0
Comment
Question by:c00kie88
  • 3
  • 2
6 Comments
 
LVL 48

Expert Comment

by:Tintin
ID: 22621173
When you use single quotes, the variables $name and $id are treated literally.

Are you actually trying to modify the contents of /etc/passwd?  If so, then I would strongly suggest you don't use this approach and use the usermod command.
0
 

Author Comment

by:c00kie88
ID: 22621865
i can;t use usemod as you need root permission..

I have to make a script for non-root user to be able to change all those fields..

I'm having a problem with sed syntax. Anyone can help me?
0
 
LVL 48

Expert Comment

by:Tintin
ID: 22622023
You would be much better off using sudo and usermod.  How is a non-root user going to make the changes anyway?

I have given you an answer to the problem with your use of sed.  Use double quotes instead of single quotes.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:c00kie88
ID: 22622055
yeah.. but it does not fix the overall problem..

Does not matter..
Thanks anyway..

I will figure it out myself

Thanks for trying
0
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
ID: 22622159
Here's the fix for your sed statement (I've tested it)


sed "s/\($user:.:[0-9]*:\)[0-9]*:\(.*\)/\1$id\2/" /etc/passwd

Open in new window

0
 
LVL 23

Expert Comment

by:Mysidia
ID: 22656333
awk -F: '/^'${name}':/ {OFS=":";  $4='${id}';  print $0   }'  /etc/passwd


Regarding earlier scripts,  the reason you're having problems:

sed 's/$name:.*:.*:$id:.*:.*:.*' /etc/passwd > logfile
^^^^^^^^^^^^^^^^^^^
this is not a well-formed substitution.
$ variables are interpreted by your shell, not sed.
you'd need to use double quotes,  single-quotes mean $name and
$id won't be expanded.. instead you're looking for a user named "$name"

Second, when performing a substitution with sed, you need two clauses,
you only have one

a sed substitution looks like this:

sed 's/XAX/XBX/'

which means "Replace the match XAX with XBX"

syntactically what your script uses is
sed 's/XAX'

So you need to terminate the first part of the s// command, then
enter the target substitution, and terminate that with a / also.





0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now