[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Nawk - SED problems

Posted on 2008-10-01
6
Medium Priority
?
328 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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 

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 500 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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 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…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

650 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