awk syntax error and question?
what I am trying to do is make a script to add users to the system.
I am reading in 2 varibaled by "read -p "Enter Username: " UN . And then like "read -p "Enter Fullname of User: " FN And my awk looks like `awk -F':' '$1 ~ "/\<'$UN'\>/" { print $0 }' /etc/passwd` I am putting in checks to either fail out if user exists or printf to null and continue. What I want or would like is for example i run it it will ask me and i enter for 1st Bob, 2nd Bob Jones. The second one i have a space in it, But the awk there delimits by a ':".. But I have tried Bob:Jones also put it prints Bob:Jones Because i have setup to echo the names and exit to make sure thins are working before it moves on to the rest of the script i have already written. I have also thought of reading in the 2 varibales has $1, and $2 ?? Any help would be appreciated thank you very much. |
I'm not sure about what the problem is. Something like
Code:
awk -F: '$5 ~ /'"$FN"'/' /etc/passwd Code:
grep ":$FN:" /etc/passwd |
ok thanks for the quick reply. Ok here it goes this is the top part of the code that i am working on and have it to exit out. Has you'll find out below i have muiltiple different ways in there that i was testing and messing around with to try and get it to work and error out correctly if needed.
set -x if [ "$?" -eq 0 ] then read -p "Enter your UserName: " UN read -p "Enter your FullName: " FN if [ "$?" -eq 0 ] then awk -F':' '$1 ~ /\<'$UN'\>/ { print $0 }' /etc/passwd #grep "$UN" /etc/passwd if [ "$?" -eq 1 ] then printf "User Doesnt Exist%s\n" #`printf > /dev/null` else echo "User Does Exist:: WARN:" fi fi fi exit Its not the cleanest, And i was just writing in some garbage to tell the different parts were spitting out errors. |
When i do the run it awk always seem to spit out a 0 even when user "Bob" didnt exist., I guess i would like to use awk maybe in less grep works better?
Did doesnt seem to work, my name is Jonathan, so if i type in "than". It will still display jonathan in the /etc/passwd file thats ofcourse is not what i what can grep do this easier? |
awk isnt going to spit out a 1 just if a regex does not match; you have to tell it to do that.
also, you cant call the variable $UN or $FN from inside an awk script. For this purpose; grep would probably be a better tool. Since a match is a zero and a non match is a 1. Please use code tags around script to preserve spacing and indentions. Code:
set -x Code:
if mycommand |
Yes. As trey85stang stated, awk returns zero if the are not error in the program execution (like syntax errors, divisions by zero and so on). You have to use an exit statement with an argument different than 0 to do otherwise. Example:
Code:
awk -F: '$1 ~ /\<'$UN'\>/{s ? s = 0 : s = 3} END{exit s}' /etc/passwd Regarding the partial match of the grep command, it is the reason I used: Code:
grep ":$FN:" /etc/passwd Code:
grep -w "$FN" /etc/passwd |
TO expand on the above comment by the moderator, consider this command list:
Code:
$ grep -0 '^[^ ]' /etc/passwd | sort -n -t: -k3 | cut -d: -f1,3,5 | tr ':' '\t' The sort just sorts the users by their UID, to make the output easier to follow. This is, of course, not necessary for your task. The cut extracts the user name, UID, and full name from the grep returned lines, and the tr is there to replace the ':' delimiter used in /etc/passwd by a tab character, again just to make the output easier to follow. <edit> Oh, this command would set the variable ${full_name} to the full name of the user ${UN}: full_name=$(grep -0 "^${UN}:" | cut -d: -f3) and you could test for a missing full_name with a if [ -z "${full_name}" ];then ... or similar construct. Note that I, as a personal choice, prefer to always use the ${variable_name} form. It prevents confusion and ambiguity. </edit> <edit2> Oops!:redface: I was just lookig at the man grep output (for another option I'd forgotten) and notices that the [-0 option was not listed, and that -z is the preferred way to specify a null as the line terminator. (Apparently -0 is a "hidden" option. </edit2> |
Perfect thanks alot I can take it from there just bit more of an understanding. I'll clean up the code. Thanks again.
|
All times are GMT -5. The time now is 01:41 AM. |