?
Solved

Nawk - SED problems

Posted on 2008-10-01
6
Medium Priority
?
329 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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses
Course of the Month17 days, 12 hours left to enroll

831 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