LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   scripting (https://www.linuxquestions.org/questions/linux-newbie-8/scripting-4175513014/)

tiggerfanatic 08-01-2014 03:20 PM

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

weibullguy 08-01-2014 04:02 PM

You're missing a closing parenthesis in your grep command. The following corrected script works for me.

Code:

#!/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


Firerat 08-01-2014 04:08 PM

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:


case $2 in
  [1-7]) do stuff..;;
      *) echo "I want number from 1 to 7 !11";;
esac


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

tiggerfanatic 08-01-2014 04:53 PM

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
To make code
Easier to
Read

I'm not sure what ya' mean?

Here is a screenshot of what I just put in vi -

http://prntscr.com/48mpep
http://prntscr.com/48mppr

Firerat 08-01-2014 05:18 PM

Code:

This is
  'Inside' [code]
example
[/code] tags

It makes the nice code boxes
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

grail 08-02-2014 12:42 AM

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)
For me the above variable will always be empty as grep never returns anything :(

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)
Also, if your code is not working, place the following as your second line (under the shebang) and check the output:
Code:

set -xv

unSpawn 08-02-2014 02:22 AM

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 $?
or grep:
Code:

grep -q -m1 "^$1" /etc/passwd; echo $?
else if you want to populate an array to immediately print items from you could:
Code:

ARRAY=($(IFS=':' /usr/bin/getent passwd "$1"))
Put together could look something like this:
Code:

#!/bin/bash -vx
# Self-explanatory
_help() { echo "You need help."; exit 1; }
# Need exactly two args
[ $# -ne 2 ] && _help
# Populate vars
_USER="$1"; _FIELDNR="$2"
# Not a number
[ -z "${_FIELDNR//[0-9]/}" ] || _help
# Wrong value
[ ${_FIELDNR} -lt 1 -o ${_FIELDNR} -gt 7 ] && _help
# All conditions met and checked lets create an array
ARRAY=($(IFS=':' /usr/bin/getent passwd "${_USER}"))
# ...and if not empty print something
[ ${#ARRAY[@]} -eq 0 ] && echo Nope || echo "${ARRAY[$[${_FIELDNR}-1]]}"
exit 0


weibullguy 08-02-2014 07:55 AM

Quote:

Originally Posted by tiggerfanatic (Post 5213429)
Weibullguy I really appreciate your response, although I still must be doing something wrong as still syntax error.

Nope, I forgot to mention the space between the $ and ( on this line
Code:

field=$ (echo $userinfo | cut -d: -f1)
That should get rid of your syntax errors.

Quote:

Originally Posted by grail
Well I am not sure how anyone has this code working as the grep regex would seem very strange:

I only meant working as in no syntax error.


All times are GMT -5. The time now is 02:49 AM.