Solved

Nawk - SED problems

Posted on 2008-10-01
6
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

691 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