Register
It is currently Wed Apr 16, 2014 9:49 pm

Using etc/passwd to extract data and add to a txt file


All times are UTC - 6 hours


Post new topic Reply to topic  [ 13 posts ] 
Author Message
 PostPosted: Fri Nov 11, 2011 2:00 pm   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
Hi guys,

I require a script that accomplishes the following please:

1) Reads a username variable input by user and checks if that username exists in field 1 of etc/passwd. If it does exist, continue script. If it doesn't, report error and try again.

2) Upon completion of step 1, I would like a script that would again check the input username against a list of already current usernames in a file I created called simple information_file. If username is found in that file, display error saying it already exists and is not unique in file. If username is not found, append the username to the file as field 1 of the new record.

3) Various othr variables such as address would be provided by user input. I'd like to know how I can sequentially apply them as fields next to the current username/record I'm working on in information_file.

That should be all for now thanks. Just for the record, I gave this script a shot using awk but have had problems. I was wondering if you could advise as to what I may be doing wrong by viewing my attachment in a later thread.

Many thanks!


Top
 Profile  
 PostPosted: Fri Nov 11, 2011 3:13 pm   

Joined: Thu Nov 10, 2011 7:31 pm
Posts: 21
awk? grep is the key to this
Code:
#! /bin/bash
user=read -p "Enter user"
function checkfile {
     if [ grep ./info '^$user ]; then
          echo "Not found in info file"
          echo "$user" >> info
          exit 0
     else
          echo "Found in info file"
          exit 1
     fi
}
if [ grep /etc/passwd '^$user ]; then
     checkfile
else echo "User $user not found in /etc/passwd/"
     exit 1
fi


Top
 Profile  
 PostPosted: Fri Nov 11, 2011 5:05 pm   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
blendmaster345 wrote:
awk? grep is the key to this
Code:
#! /bin/bash
user=read -p "Enter user"
function checkfile {
     if [ grep ./info '^$user ]; then
          echo "Not found in info file"
          echo "$user" >> info
          exit 0
     else
          echo "Found in info file"
          exit 1
     fi
}
if [ grep /etc/passwd '^$user ]; then
     checkfile
else echo "User $user not found in /etc/passwd/"
     exit 1
fi


Sorry man but I'm getting an unexpected end of file error?


Top
 Profile  
 PostPosted: Fri Nov 11, 2011 5:09 pm   

Joined: Thu Nov 10, 2011 7:31 pm
Posts: 21
Code:
#! /bin/bash
user=read -p "Enter user"
function checkfile {
     if [ grep ./info '^$user' ]; then
          echo "Not found in info file"
          echo "$user" >> info
          exit 0
     else
          echo "Found in info file"
          exit 1
     fi
}
if [ grep /etc/passwd '^$user' ]; then
     checkfile
else echo "User $user not found in /etc/passwd/"
     exit 1
fi


Top
 Profile  
 PostPosted: Fri Nov 11, 2011 5:49 pm   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
blendmaster345 wrote:
Code:
#! /bin/bash
user=read -p "Enter user"
function checkfile {
     if [ grep ./info '^$user' ]; then
          echo "Not found in info file"
          echo "$user" >> info
          exit 0
     else
          echo "Found in info file"
          exit 1
     fi
}
if [ grep /etc/passwd '^$user' ]; then
     checkfile
else echo "User $user not found in /etc/passwd/"
     exit 1
fi


Binary Operator Expected error on line 18? :(


Top
 Profile  
 PostPosted: Fri Nov 11, 2011 7:21 pm   

Joined: Thu Nov 10, 2011 7:31 pm
Posts: 21
I'm no good with grep, but it should definitely be used in this case. What are you trying to do anyways?


Top
 Profile  
 PostPosted: Fri Nov 11, 2011 8:18 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 511
Code:
grep pattern file

Code:
if grep '^$user' /etc/passwd; then
no need of [ ]


Top
 Profile  
 PostPosted: Fri Nov 11, 2011 8:35 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Code:
#!/bin/bash                                                                                                                   
# INFO FILE PATH
info_file='information_file'


if [[ ! -f $info_file ]]; then
    printf '« %s »: no such file found.\n' "$info_file" >&2
    exit 1
fi

exec 3<> "$info_file"


ask()
{
    if ((! $#)); then
        return 1
    fi 
       
    read -rp "$@ "
}

ask_var()
{
    if (($# != 2)); then
        return 1
    fi 
       
    read -rp "$1 " "$2"
}


is_in_array()
{
    local str=$1 elmt
    shift

    for elmt; do
        if [[ $str = "$elmt" ]]; then
            return
        fi
    done

    return 1
}

write_to_file()
{
    if ((! $#)); then
        return 1
    fi

    printf '%s ' "$@" >&3
}
   
writeln_to_file()
{
    if ((! $#)); then
        return 1
    fi

    printf '%s\n' "$@" >&3
}


while IFS=: read -r p_u _; do
    passwd_users+=("$p_u")
done < <(getent passwd)

ask 'Username:'

while ! is_in_array "$REPLY" "${passwd_users[@]}"; do
    printf "The user « %s » doesn't exist.\n\n" "$REPLY"
    ask 'Username:'
done

while read -u 3 -r i_u _; do
    info_users+=("$i_u")
done
   
if is_in_array "$REPLY" "${info_users[@]}"; then
    printf 'The user « %s » already exists ' "$REPLY"
    printf "and isn't unique in « %s ».\n" "$info_file"
    exit
fi

write_to_file "$REPLY"

ask_var 'Stuff:' stuff
write_to_file "$stuff"

# ...

ask_var 'Address:' add
writeln_to_file "$add"

exec 3>&-


Top
 Profile  
 PostPosted: Sat Nov 12, 2011 6:44 am   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
blendmaster345 wrote:
I'm no good with grep, but it should definitely be used in this case. What are you trying to do anyways?


It's part of a course exercise I'm meant to research and complete before the actual assignment comes up.


Top
 Profile  
 PostPosted: Sat Nov 12, 2011 6:59 am   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
Watael wrote:
Code:
grep pattern file

Code:
if grep '^$user' /etc/passwd; then
no need of [ ]


Thanks for the modification Watael, it at least got rid of the errors :) There's just one little problem left... The if statement still evaluates that my username doesn't exist in etc/passwd, even though some did ¬¬. Again, please help if you can, it truly is appreciated :)) . If you're curious, my if statement looks as follows:

Code:
if grep '^$username' etc/passwd; then
             checkfile
          else
             echo "$username doesn't exist in etc/passwd"
             exit 1
           fi


Top
 Profile  
 PostPosted: Sat Nov 12, 2011 2:38 pm   
User avatar

Joined: Tue Apr 27, 2010 2:28 pm
Posts: 172
Location: Czech Republic
jjb1989 wrote:
Code:
if grep '^$username' etc/passwd; then
             checkfile
          else
             echo "$username doesn't exist in etc/passwd"
             exit 1
           fi

Single quotes do not interpolate variables. Use double quotes.


Top
 Profile  
 PostPosted: Sat Nov 12, 2011 6:51 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
Doesn't the code I posted earlier do what you want, jjb1989?

Since you haven't told us anything about information_file's structure,
I assumed that it looked like that:
Code:
user1
user2
[...]


I may be wrong.


Top
 Profile  
 PostPosted: Sun Nov 13, 2011 9:37 am   

Joined: Fri Nov 11, 2011 1:40 pm
Posts: 8
jsz wrote:
Doesn't the code I posted earlier do what you want, jjb1989?

Since you haven't told us anything about information_file's structure,
I assumed that it looked like that:
Code:
user1
user2
[...]


I may be wrong.


Sorry jsz. Your code did work and you have the right idea about information_file. I'd just have a hard time explaining the code you provided to my lecturer as I myself have no clue. I'm not advanced enough in the Bash field yet as you guys probably guessed by now :|


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 9 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP