Solved

Nawk - SED problems

Posted on 2008-10-01
6
323 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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
init script for deployment on linux machine 5 53
whats a headless user in linux 3 377
check failing on yum update 5 32
can't run or update 'yum' on CentOS 7 7 74
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

763 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