LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-11-2014, 12:17 AM   #1
STAN71
LQ Newbie
 
Registered: May 2014
Posts: 6

Rep: Reputation: Disabled
bash scripting


hello guys!
I'm newbie in linux bash scripting. Recently taking class Unix/Linux OS's where our instructor started from bash scripting. On the week 2 we already had to learn loops, that just to show how fast he was going through the course. And of course, majority of students got no clue what they do, including present party. I have to finish my last assignment on bash scripting, all class copied scripts letter by letter from the "board" when our instructor was running scripts for this particular assignment, but it doesn't work .
Would you please take a look at the scripts and tell me what is wrong and what needs to be changed?
The goal of this script is to display user's directories and files in those directories.But instead, that gives me directories of the whole system, all it's files and then at the end of it display users.
Thank you in advance.
Stokil.
Here is my code:
#!/bin/bash
# Name: userreport
# By: Stokil
# Date: 6/4/2014
# Purpose: report of all users on system
########################################
clear
x=500
while [ $x -lt 510 ]
do
user=`grep ":x:$x:" /etc/passwd | cut -f1 -d:`


echo "#####################################"
echo "$user"
echo "#####################################"
echo " "

echo "Home Directory"
ls -ld `grep "$user" /etc/passwd | cut -f6 -d":"`
echo " "
echo "Estimate Directory Size"
echo "======================="
du -h -s /home/$user | cut -f6 -d":" | cut -f1 -d"/"
echo " "
echo "Display the User information"
echo "============================"
grep "$user" /etc/passwd
echo " "
echo "Shadow file"
echo "==========="
grep "$user" /etc/shadow | cut -f2 -d":"
echo " "
echo "Groups"
echo "======"
grep "$user" /etc/group | cut -f1 -d":"
echo " "
echo "Last Login"
echo "===="
last $user
echo "ls"
tree -d `grep "$user" /etc/passwd |cut -f6 -d":"`

echo "============================"
echo " "
let x=$x+1
done
 
Old 06-11-2014, 01:31 AM   #2
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
1. instead of grep "$user" /etc/passwd | cut .... you can write: awk -F: '/'$user'/ { print $1 }' /etc/passwd # replace $1 to $2, $3, $NF, whatever you need
2. you grep the home dir from /etc/passwd but later you use /home/$user (instead of the collected data)
3. use variable to store home dir
4. group info can be retrieved by the command groups <userid>

use [code]here comes your script[/code] to keep formatting
 
Old 06-11-2014, 02:01 AM   #3
STAN71
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
1. instead of grep "$user" /etc/passwd | cut .... you can write: awk -F: '/'$user'/ { print $1 }' /etc/passwd # replace $1 to $2, $3, $NF, whatever you need
2. you grep the home dir from /etc/passwd but later you use /home/$user (instead of the collected data)
3. use variable to store home dir
4. group info can be retrieved by the command groups <userid>

use [code]here comes your script[/code] to keep formatting
I'm really sorry for the stupid question, but should i do awk command even after while "do"? Or that has to stay as grep?
I like Linux and that was the main reason for taking that class, but here is a link on reviews and average grades people get in this class. You ether has to be a linux gig already or you got nothing
https://www.myedu.com/MHCC-Mt-Hood-C...ourse/2313014/
Thank you for reply
 
Old 06-11-2014, 02:14 AM   #4
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
I do not really care about that class, and you can reach any level (of knowledge) - if you want to do so. So there is no stupid question, just ask and learn and practice and you will win.

If you are not really familiar with shell scripting and awk I suggest you to do only small steps, little modifications. For example try this line: grep "$user" /etc/shadow | cut -f2 -d":" and replace only this with an awk script.

see man pages also (for example man awk or man bash), or:
http://www.tldp.org/LDP/abs/html/
 
Old 06-11-2014, 02:22 AM   #5
STAN71
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
I do not really care about that class, and you can reach any level (of knowledge) - if you want to do so. So there is no stupid question, just ask and learn and practice and you will win.

If you are not really familiar with shell scripting and awk I suggest you to do only small steps, little modifications. For example try this line: grep "$user" /etc/shadow | cut -f2 -d":" and replace only this with an awk script.

see man pages also (for example man awk or man bash), or:
http://www.tldp.org/LDP/abs/html/
thank you,
I did replace some of the grep commands for awk, but it still gives me a tons of info i don't need
here is some of the very bottom of the script:
| `-- usage
`-- yp
`-- binding
/var/lib/avahi-autoipd [error opening dir]
/var/gdm
/home/sabayon [error opening dir]
/home/stanislav
`-- Desktop
/home/mikeb
/home/gregb
|-- home
`-- school
|-- arithmetic
|-- reading
`-- writing
/home/peterb
|-- home
`-- school
|-- arithmetic
|-- reading
`-- writing
/home/bobbyb
|-- home
`-- school
|-- arithmetic
|-- reading
`-- writing
/home/marshab
|-- home
`-- school
|-- arithmetic
|-- reading
`-- writing
/home/janb
|-- home
`-- school
|-- arithmetic
|-- reading
`-- writing
/home/cindyb
|-- home
`-- school
|-- arithmetic
|-- reading
`-- writing
/home/carolb
|-- home
`-- school
|-- arithmetic
|-- reading
`-- writing

107309 directories
as you can see the users start at /home/stanislav and below. But the stuff above it, that is what i do not need. It is in /etc/passwd directory, but how to get rid of it?
I'm ready to learn.

Thank you very much Mr. Pan64 for your help. I'll try to mess with it a little more tomorrow. And also appreciate for useful link.
Have a good DAY.

Last edited by STAN71; 06-11-2014 at 02:47 AM.
 
Old 06-11-2014, 02:46 AM   #6
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
there was something wrong at the beginning I do not understand.
You may try to skip unwanted users
also you can insert set -xv at the beginning and you will see how that script works
 
Old 06-11-2014, 02:52 AM   #7
STAN71
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
there was something wrong at the beginning I do not understand.
You may try to skip unwanted users
also you can insert set -xv at the beginning and you will see how that script works
I tried to limited users to 2 and it still gives me all info above. Looks like it doesn't respond for x=500
Just tried to insert set -xv, still have the same result. Why, in your opinion it would read not users only, but everything that includes /home/?
should i exclude root? If so, how? Here is part of the output i have:
| | | |-- errors
| | | | |-- Armenian
| | | | |-- Azerbaijani
| | | | |-- Bulgarian
| | | | |-- Catalan
| | | | |-- Czech
| | | | |-- Danish
| | | | |-- Dutch
| | | | |-- English
| | | | |-- Estonian
| | | | |-- Finnish
| | | | |-- French
| | | | |-- German
| | | | |-- Greek
| | | | |-- Hebrew
| | | | |-- Hungarian
| | | | |-- Italian
| | | | |-- Japanese
| | | | |-- Korean
| | | | |-- Lithuanian
| | | | |-- Polish
| | | | |-- Portuguese
| | | | |-- Romanian
| | | | |-- Russian-1251
| | | | |-- Russian-koi8-r
| | | | |-- Serbian
| | | | |-- Simplify_Chinese
| | | | |-- Slovak
| | | | |-- Spanish
| | | | |-- Swedish
| | | | |-- Traditional_Chinese
| | | | |-- Turkish
| | | | |-- Ukrainian-1251
| | | | |-- Ukrainian-koi8-u
| | | | `-- Ukrainian-utf8
| | | `-- icons
As far as i understand this is in a root directory and i'm logged in as a root.

Last edited by STAN71; 06-11-2014 at 02:58 AM.
 
Old 06-11-2014, 03:33 AM   #8
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian i686 (solaris)
Posts: 8,104

Rep: Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267Reputation: 2267
you need to insert the line set -xv at the beginning of the script:
#!/bin/bash
set -xv
.....

and when you run it you will see a lot of information about the execution. Just check it line by line to understand what's happening.
set -xv will not influence/modify the execution itself just print a lot of information about that.

Try this instead of that while loop:
Code:
for u in `awk -F: '{print $3}' /etc/passwd`;
do
# skip user
   [[ $u -eq 0 ]] && continue
  # your code
done
 
Old 06-11-2014, 03:48 PM   #9
fotoguy
Senior Member
 
Registered: Mar 2003
Location: Brisbane Queensland Australia
Distribution: KirraMail Live Email Server
Posts: 1,285

Rep: Reputation: 61
I know i'm not answering your post buy this is a link to a beginners guide to bash scripting that will help give you a good understanding of bash scripting, not sure if your teacher has given you something like this already, I myself have the advanced bash scripting tuttorial pdf printed for me which I use for referencing all the time, even after 14 years of using linux.

http://www.tldp.org/LDP/Bash-Beginne...ners-Guide.pdf

Hope this helps
 
Old 06-11-2014, 07:53 PM   #10
STAN71
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by fotoguy View Post
I know i'm not answering your post buy this is a link to a beginners guide to bash scripting that will help give you a good understanding of bash scripting, not sure if your teacher has given you something like this already, I myself have the advanced bash scripting tuttorial pdf printed for me which I use for referencing all the time, even after 14 years of using linux.

http://www.tldp.org/LDP/Bash-Beginne...ners-Guide.pdf

Hope this helps
No, my instructor didn't give us anything about bash scripting. It suppose to be a linux server class.
Thank you very much for this link.
 
Old 06-12-2014, 02:19 AM   #11
STAN71
LQ Newbie
 
Registered: May 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
you need to insert the line set -xv at the beginning of the script:
#!/bin/bash
set -xv
.....

and when you run it you will see a lot of information about the execution. Just check it line by line to understand what's happening.
set -xv will not influence/modify the execution itself just print a lot of information about that.

Try this instead of that while loop:
Code:
for u in `awk -F: '{print $3}' /etc/passwd`;
do
# skip user
   [[ $u -eq 0 ]] && continue
  # your code
done
Thank you for your help. I finally found solution for the problem i had with this script. There was users numbers not in sequence.
Here what i added to the script to solve this problem:
x=500
while [ $x -lt 509 ]
do
user=`grep ":x:$x:" /etc/passwd | cut -f1 -d":"`

let x=$x+1

if [[ $user == "" ]]; then
#echo "group_id=$x not found in /etc/passwd";
continue;
fi
The rest of the script I left same as it was except let x=$x+1
Because I place it at the beginning.
Now it works like a charm.
 
  


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
[SOLVED] Help with bash scripting jim.thornton Linux - Newbie 7 04-25-2012 02:32 PM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 03:29 AM
bash scripting.. kurrupt Programming 3 09-21-2005 01:07 AM
Bash Scripting help jgtg32a Programming 5 09-06-2005 10:38 AM
bash scripting vadon Linux - Newbie 6 05-10-2005 05:07 AM


All times are GMT -5. The time now is 04:33 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