- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- AWK scripting question
Operating System - HP-UX
1755155
Members
4922
Online
108830
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2000 05:04 AM
тАО09-19-2000 05:04 AM
Hi everyone,
I'm attempting to automatically generate a new UID for a user created through my script. I'm attempting to do this by using awk to run through the /etc/passwd file, and set a variable to one higher than what it finds in field three (assuming field three is higher than the current value). I do realize there are a few ways of doing this without awk, but since I don't know awk that well(or at least didn't before starting this script :) I thought I'd try and use it.
The problem I'm actually having is how to get the final value of the variable out of awk and into the main script. If I print the variable inside of the awk statement I get the correct values, but if I echo the variable after the awk statement I get nothing. The following is my awk statement:
awk -F: '{if ($3 >= UID) { UID=$3; UID++;}}' /etc/passwd
Anyone have any ideas? Again I'd like to stick with awk if possible.
Thanks,
Steve
I'm attempting to automatically generate a new UID for a user created through my script. I'm attempting to do this by using awk to run through the /etc/passwd file, and set a variable to one higher than what it finds in field three (assuming field three is higher than the current value). I do realize there are a few ways of doing this without awk, but since I don't know awk that well(or at least didn't before starting this script :) I thought I'd try and use it.
The problem I'm actually having is how to get the final value of the variable out of awk and into the main script. If I print the variable inside of the awk statement I get the correct values, but if I echo the variable after the awk statement I get nothing. The following is my awk statement:
awk -F: '{if ($3 >= UID) { UID=$3; UID++;}}' /etc/passwd
Anyone have any ideas? Again I'd like to stick with awk if possible.
Thanks,
Steve
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2000 05:09 AM
тАО09-19-2000 05:09 AM
Re: AWK scripting question
The following will work:
cat /etc/passwd | awk -F: '{if (($3 > UID)) print $3}' | sort -n | tail -1
This is going to awk out the UID field greater than some number UID. The sort will put into ascending order and the tail will grab the last line. To this, you can use the `expr` command and add 1 to the value to get a new UID number.
cat /etc/passwd | awk -F: '{if (($3 > UID)) print $3}' | sort -n | tail -1
This is going to awk out the UID field greater than some number UID. The sort will put into ascending order and the tail will grab the last line. To this, you can use the `expr` command and add 1 to the value to get a new UID number.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2000 05:15 AM
тАО09-19-2000 05:15 AM
Solution
I would use something like:
ID = `your_awk_script` or
ID = $(your_awk_script)
After this ID should contain the value you print with you awk script. Inside you awk script i would put something like:
... END{print UID}
This should print the highest UID at the end of the awk script.
Let me know if this helped.
Regards Stefan
ID = `your_awk_script` or
ID = $(your_awk_script)
After this ID should contain the value you print with you awk script. Inside you awk script i would put something like:
... END{print UID}
This should print the highest UID at the end of the awk script.
Let me know if this helped.
Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-19-2000 05:37 AM
тАО09-19-2000 05:37 AM
Re: AWK scripting question
Thanks guys,
Although I believe either would have worked I went with the solution Stefan suggested. It was actually something I had tried, but I had neglected to put in the print statement (oops). Thank you both for your help.
Steve
Although I believe either would have worked I went with the solution Stefan suggested. It was actually something I had tried, but I had neglected to put in the print statement (oops). Thank you both for your help.
Steve
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP