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 03-31-2012, 08:21 PM   #1
JJX
Member
 
Registered: Mar 2004
Location: Greece
Distribution: Debian
Posts: 343

Rep: Reputation: 31
Bash commands


Hi all, I am trying to create a few simple bash scripts but I face a few problems

* How can I find the home directory & login hell of a user ?
Only by parsing /etc/passwd?


* I am trying to make a list with user's group.
[root@localhost ~]# id root
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

Is any way to get only groups and avoid additonal text manipulation?!

Thank you
 
Old 03-31-2012, 08:53 PM   #2
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 6,976

Rep: Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283Reputation: 1283
If you are trying to write "simple" bash scripts, the simplest thing to do is to post the content here along with a note stating what you expect the program to do and what it actually does.

Quote:
How can I find the home directory & login hell of a user ?
????

Quote:
I am trying to make a list with user's group.
A list of what? Users?
Posting what your specific problems are would probably be the easiest way to get help as there are hundreds of other members here at LQ who are very proficient with bash scripts.
 
Old 03-31-2012, 09:42 PM   #3
TommyC7
Member
 
Registered: Mar 2012
Distribution: Slackware, CentOS, OpenBSD, FreeBSD
Posts: 454

Rep: Reputation: Disabled
Quote:
JJX:
Hi all, I am trying to create a few simple bash scripts but I face a few problems

* How can I find the home directory & login hell of a user ?
Only by parsing /etc/passwd?


* I am trying to make a list with user's group.
[root@localhost ~]# id root
uid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

Is any way to get only groups and avoid additonal text manipulation?!

Thank you
env or printenv has the globally set environment variables for the user you're currently logged in as.

$HOME or even ~ works. (e.g. ~/Documents leads to /home/user/Documents, and so would $HOME/Documents).

The login shell variable under printenv is SHELL, so echo $SHELL would print their default login shell.

As for the groups thing, the command "groups" shows the groups a user is a part of. Without anything it's the user you're logged in as, but "groups username" would show the groups that user "username" is under.

Last edited by TommyC7; 03-31-2012 at 10:52 PM.
 
Old 03-31-2012, 10:31 PM   #4
Fabio Paolini
Member
 
Registered: Dec 2008
Location: Brazil
Distribution: Slackware 12 Debian 5
Posts: 52

Rep: Reputation: 17
To get only the group as executing the id command use the -g parameter:
Code:
 id -g root
0
Code:
man id
may show more details
 
Old 03-31-2012, 10:58 PM   #5
ALB
LQ Newbie
 
Registered: Mar 2012
Posts: 10

Rep: Reputation: Disabled
This will just give the users list of group names:

Code:
# id -Gn root
root bin daemon sys disk wheel
If you need just the numbers you can remove the n:

Code:
# id -G root
0 1 2 3 6 10
 
Old 04-01-2012, 12:01 AM   #6
rajkumar.m
LQ Newbie
 
Registered: May 2010
Posts: 18

Rep: Reputation: 12
The ways to find HOME directory

env | grep -i home

echo $HOME

printenv | grep -i home

cat /etc/passwd | grep -i <username> | cut -d: -f6


The ways to find user's login shell

echo $0

echo $SHELL

The ways to find user's group

id -g <username>

cat /etc/passwd | grep -i <username> | cut -d: -f4

cat /etc/passwd | grep -i ^rajm | cut -d: -f3
 
Old 04-01-2012, 05:25 AM   #7
JJX
Member
 
Registered: Mar 2004
Location: Greece
Distribution: Debian
Posts: 343

Original Poster
Rep: Reputation: 31
I was looking for:

id -Gn root
&
I was searching for an alternative way for this:

cat /etc/passwd | grep -i <username> | cut -d: -f4

but doesnt seem to exist.

Thank you!!!
 
1 members found this post helpful.
Old 04-01-2012, 09:21 AM   #8
roopakl
Member
 
Registered: Sep 2011
Posts: 92

Rep: Reputation: Disabled
The below script may help you to find out the user's home directory, login shell, and his/her UID GID.
Code:
#!/bin/bash
usage="Usage: $0 username "
if [ $# -ne 1 ]; then
        echo $usage
        exit 1
fi
username=$1
grep $username /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
home=$(grep $username /etc/passwd | awk -F':' {'print $6'})
shell=$(grep $username /etc/passwd | awk -F':' {'print $7'})
ugid=$(grep $username /etc/passwd | awk -F':' {'print $3 " " $4'})
echo "Home directory for $username is \"$home\" "
echo "Login shell for $username is \"$shell\" "
echo "UID and GID for $username is \"$ugid\" "
else
echo $username does not exist!!!
fi
 
Old 04-04-2012, 06:10 PM   #9
JJX
Member
 
Registered: Mar 2004
Location: Greece
Distribution: Debian
Posts: 343

Original Poster
Rep: Reputation: 31
A few more questions:

* if commands "members" is not availble (i can install it to debian but readhat doesnt have it), how can I find the members of a group ?
Is there an easy way? "cat /etc/group | grep <goup_name>" is enough ?

* Also, if I have a lot files (file01 up to file100) how can I match 20 up to 100 ?
file[20-100] does not function as I expect.

Anyone has any documents for file globbing with complex examples/exercises?


Thank you
 
Old 04-04-2012, 08:37 PM   #10
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Code:
grep <grpname> /etc/group
Useful docs for bash:
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

Read & enjoy
 
  


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
Where are BASH commands stored? Not scripts but what bash exe uses. theKbStockpiler Programming 11 02-23-2011 04:06 PM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 09:56 AM
Running bash but common bash commands not working michellepace Linux - Newbie 11 07-17-2010 10:19 AM
Bash Commands chili811 Linux - Newbie 4 03-04-2009 12:05 PM
Bash commands PhuckFonix Linux - Newbie 2 03-30-2004 08:13 PM


All times are GMT -5. The time now is 11:01 AM.

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