scripting
I am just now trying my best to learn about scripting and I know I must've done something wrong (whether or not I maybe didn't put a space where one belonged or something else I don't know), 'cause when I entered this in the command line it came up w/ a syntax error? This is what the scripting problem was and this is what I had put in -
Write a script called my.passwd which will take a username and a 2nd variable (number) as input. It will search /etc/passwd for the username and then return the name of the field and field info for the number. It must check to make sure you passed in a username and number and that the username is a valid name on the system. Fields are 1) username, 2) password, 3) UID, 4) GID, 5) comment, 6) home, 7) shell I put this in the command line - oh sorry through vi - #!/bin/bash userinfo=$(grep ^{1}: /etc/passwd if [ -n "$userinfo" ] then case $2 in 1) field=$ (echo $userinfo | cut -d: -f1) echo "${1} 's username=field" ;; 2) *echo "Needs to be a number between 1-7" ;; esac else echo "Username not found in /etc/passwd" fi |
You're missing a closing parenthesis in your grep command. The following corrected script works for me.
Code:
#!/bin/bash |
Firstly, please use
[code]tags To make code Easier to Read[/code] At the moment your script checks the usernsme ( first option passed $1 ) It then checks the second option ($2) if it is 1, 'prints' username If 2, says it should be a number 1-7 Not what you want. Code:
I don't have links .. but look at my last 10 posts, a few have links for bash guides/manuals edit copy'n'paste http://www.tldp.org/LDP/Bash-Beginners-Guide/html/ http://www.tldp.org/LDP/abs/html/ http://mywiki.wooledge.org/BashGuide http://www.gnu.org/software/bash/manual/bashref.html |
Weibullguy I really appreciate your response, although I still must be doing something wrong as still syntax error. :( And firerat not sure what you mean by Firstly, please use
Code:
tags Here is a screenshot of what I just put in vi - http://prntscr.com/48mpep http://prntscr.com/48mppr |
Code:
This is fixed width font, and more than one 'space' ( normal posts 'strip' extra spaces and tabs! look at http://www.linuxquestions.org/questi....php?do=bbcode |
Well I am not sure how anyone has this code working as the grep regex would seem very strange:
Code:
userinfo=$(grep ^{1}: /etc/passwd) Either you are looking for a single carat followed by a colon (which wouldn't work without the carat being escaped and grep using -E) or you are missing the dollar sign from the front of the parameter name. I would also add that not quoting what is being grepped is fraught with danger as well. Assuming it is to be a parameter, I would use: Code:
userinfo=$(grep "^${1}:" /etc/passwd) Code:
set -xv |
If you only need to confirm a user name exists then you could use the exit value of the 'getent' command:
Code:
/usr/bin/getent passwd "$1" >/dev/null 2>&1; echo $? Code:
grep -q -m1 "^$1" /etc/passwd; echo $? Code:
ARRAY=($(IFS=':' /usr/bin/getent passwd "$1")) Code:
#!/bin/bash -vx |
Quote:
Code:
field=$ (echo $userinfo | cut -d: -f1) Quote:
|
All times are GMT -5. The time now is 02:49 AM. |