LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-01-2014, 04:20 PM   #1
tiggerfanatic
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Rep: Reputation: Disabled
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
 
Old 08-01-2014, 05:02 PM   #2
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
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
 
Old 08-01-2014, 05:08 PM   #3
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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

Last edited by Firerat; 08-01-2014 at 05:17 PM.
 
Old 08-01-2014, 05:53 PM   #4
tiggerfanatic
LQ Newbie
 
Registered: Jul 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Unhappy

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
 
Old 08-01-2014, 06:18 PM   #5
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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
 
Old 08-02-2014, 01:42 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,425

Rep: Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826Reputation: 2826
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
 
Old 08-02-2014, 03:22 AM   #7
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,332
Blog Entries: 55

Rep: Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533Reputation: 3533
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
 
Old 08-02-2014, 08:55 AM   #8
weibullguy
ReliaFree Maintainer
 
Registered: Aug 2004
Location: Kalamazoo, Michigan
Distribution: Slackware-current, Cross Linux from Scratch, Gentoo
Posts: 2,812
Blog Entries: 1

Rep: Reputation: 259Reputation: 259Reputation: 259
Quote:
Originally Posted by tiggerfanatic View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Scripting pr0xibus Programming 3 02-17-2014 03:56 PM
LXer: Scripting the Linux desktop, Part 2: Scripting Nautilus LXer Syndicated Linux News 0 02-17-2011 05:02 AM
Firefox Scripting Add-on (Scripting HTML / Javascript inside Firefox) linuxbeatswindows Programming 1 09-18-2009 11:09 PM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
scripting help please deepsix Programming 10 09-08-2005 08:49 PM


All times are GMT -5. The time now is 11:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration