LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-20-2017, 09:09 AM   #1
Justaguy123
LQ Newbie
 
Registered: Dec 2017
Posts: 7

Rep: Reputation: Disabled
Bash script


Hello
I need help with bash scripting
" script finds for each user(chosen users/groups) files which belong to them and other users have access to those files(optional specifically groups) and mail to user list of these files. "

I dont use bash normally and in work i'm actually doing 3 large projects so i dont have enough time for doing this. Can someone help me? I know its easy for you but i'm not familar with programming and those things

sorry for my english
 
Old 12-20-2017, 09:17 AM   #2
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,481

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
So what have you done so far?

This is my prefered bash reference http://tldp.org/LDP/abs/html/index.html
 
1 members found this post helpful.
Old 12-20-2017, 09:19 AM   #3
Justaguy123
LQ Newbie
 
Registered: Dec 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
I didn't even started. It's very confusing when i read about it, i had only PHP and HTML basics ;(
 
Old 12-20-2017, 09:25 AM   #4
TenTenths
Senior Member
 
Registered: Aug 2011
Location: Dublin
Distribution: Centos 5 / 6 / 7
Posts: 3,481

Rep: Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553Reputation: 1553
Code:
man find
man grep
man awk
Then feel free to show what you've tried and to ask specific questions.

Hints grep /etc/passwd to find / verify a user exists, use awk to get the user id and then read about the find command.

Last edited by TenTenths; 12-20-2017 at 10:43 AM.
 
1 members found this post helpful.
Old 12-20-2017, 09:45 AM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,685

Rep: Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972Reputation: 7972
Quote:
Originally Posted by Justaguy123 View Post
I didn't even started. It's very confusing when i read about it, i had only PHP and HTML basics ;(
Then if you already know about PHP and HTML coding, this shouldn't be difficult to learn. But you need to read the "Question Guidelines" link in my posting signature. We are always happy to help you, but we WILL NOT write your scripts for you. You have to some at least SOME effort of your own. Post what you have written/done/tried and tell us where you're stuck.

Otherwise, lots of bash scripting tutorials/examples you can easily find with an internet search...much like how you found this site. And we will never do your homework for you either.
 
Old 12-20-2017, 10:38 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Justaguy123 View Post
Hello
I need help with bash scripting
" script finds for each user(chosen users/groups) files which belong to them and other users have access to those files(optional specifically groups) and mail to user list of these files. "

I dont use bash normally and in work i'm actually doing 3 large projects so i dont have enough time for doing this. Can someone help me? I know its easy for you but i'm not familar with programming and those things

sorry for my english
Quote:
Originally Posted by Justaguy123 View Post
I didn't even started. It's very confusing when i read about it, i had only PHP and HTML basics ;(
OK well this seems to be part of your work and to me you should devote some time to learn how to use bash.

The intentions of LQ are that we are not paid support, we are all volunteers and we are here to help you, but also help you to learn about Linux, not to do your effort for you. Continued behavior where you ask people to just complete your work for you may result in you not being able to participate on the LQ site.

A few members have offered some means to get going, and here is another one from my blog, Bash Scripting for Dummies and Geniuses

Also my favorite (self) quote:
Quote:
"Whatever you can type on a command line, you can write in a script."

Last edited by rtmistler; 12-20-2017 at 10:40 AM.
 
2 members found this post helpful.
Old 12-21-2017, 04:38 AM   #7
Justaguy123
LQ Newbie
 
Registered: Dec 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
#!/bin/bash


users=($(awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd))
touch tmp
for user in $users
do
echo "###############################################">> tmp
echo "@@@@@@@@@@@@@@@@@@@@ $user @@@@@@@@@@@@@@@@@@@@">> tmp
echo "###############################################">> tmp
echo " ">> tmp
find / -type f -user $user -perm /333 >> tmp
echo " ">> tmp
done


this is what i made and i stucked
 
Old 12-21-2017, 06:58 AM   #8
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Quote:
Originally Posted by Justaguy123 View Post
#!/bin/bash


users=($(awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd))
touch tmp
for user in $users
do
echo "###############################################">> tmp
echo "@@@@@@@@@@@@@@@@@@@@ $user @@@@@@@@@@@@@@@@@@@@">> tmp
echo "###############################################">> tmp
echo " ">> tmp
find / -type f -user $user -perm /333 >> tmp
echo " ">> tmp
done


this is what i made and i stucked
That script works. What exactly do you mean by it got stuck? Are you saying it never returns back to the prompt?

A couple of points:
  1. Add "set -xv" as the second line of your script. This will enable verbose debug and show you what is happening.
  2. If you are a regular user running this script, the find command is using the '/' directory and there will be a lot of files and directories where the command will not work, due to your permissions. There are options to use sudo and set up sudoers so that you would not have to use a password.
  3. In the future, when posting code, please place it within [code][/code] tags to properly maintain the spacing and formatting.
  4. Once again, this posted script, does work correctly. It however does not do exactly what you described. This script only finds the files owned by the current user, which have a specific permission.

Last edited by rtmistler; 12-21-2017 at 06:59 AM.
 
Old 12-21-2017, 07:07 AM   #9
Justaguy123
LQ Newbie
 
Registered: Dec 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thanks you!

Actually my problem is that running this script with no parameters is supposed to do list of files for every "human-made" accounts so there is any way to use this script in loop to run it for every account in OS and find how to find out that someone have access to file of these accounts? I must in first step find all files which i posses and then check permissions? My goal is in the first post

Last edited by Justaguy123; 12-21-2017 at 07:13 AM.
 
Old 12-21-2017, 07:21 AM   #10
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,883
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Enter the loop with the entire list of users from /etc/passwd, do not filter it down prior to entering the loop.
When inside the loop, perform your if test to determine if the id is greater than or equal to 1000.
If yes, then perform the find. Otherwise, no action.

I believe what your script is doing is that it takes /etc/passwd once, checks it once, finds the first occurrence of a user matching your if-test qualifications of >= 1000 && !65534, and that's it. It only ever finds the first occurrence.

When you enter your for loop, you have one entry in your list.

Instead make the list $users be the entirety of /etc/passwd.
Enter the loop.
And then perform your if-test and use that outcome to run or not run the find command.
 
Old 12-22-2017, 12:13 AM   #11
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
on my system it finds 2 users.

the script seems to be doing what it is supposed to do: find all files that belong to a certain user and spit them out.
the permission thingy is new to me; i hope it finds what it is supposed to find.
of course there is no mailing yet.

a few points:
  1. if not performed with su privileges, it will spit out a lot of permissiondenied errors, hence i added "2>/dev/null"
  2. "tmp" is not a good filename, because it might well ne in use already. use something more descriptive.
  3. users doesn't have to be an array afaics, but should be enclosed in doublequotes.
here's what i used for testing:
Code:
#!/bin/bash


users="$(awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd)"

echo $users 

for user in $users
do
	echo "$user:" && find / -type f -user $user -perm /333 2>/dev/null
done
 
Old 12-22-2017, 03:50 AM   #12
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,806

Rep: Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207Reputation: 1207
Scanning / is expensive.
I would do this once and save the output in a file.
A -ls keeps the user information along with the file names.
Then loop over the users and grep them in the file.

Example script:
Code:
#!/bin/bash
# set the PATH (do not inherit from environment)
export PATH=/bin:/usr/bin:/sbin:/usr/sbin
# variables, allows an easy change here
startdir=/
badfiles=badfiles

# create badfiles only if +24 hours old
if [ ! -s $badfiles ] || find $badfiles -prune -mtime +0 | grep .
then
  echo "scanning / and creating $badfiles"
  find $startdir -type d -name .snapshot -prune -o -type f -perm /033 -ls > $badfiles
fi

# criteria for valid users: UID range and /home/ directory
users=$(getent passwd | awk -F':' '{if ($3 >= 1000 && $3 != 65534 && $6 ~ /^\/home\//) print $1}')

# look up each user in badfiles
for user in $users
do
  awk -v user="$user" '$5==user { if (!title) { print "Bad files for", user, ":"; title=1 } print }' $badfiles
done
 
Old 12-22-2017, 08:37 AM   #13
Justaguy123
LQ Newbie
 
Registered: Dec 2017
Posts: 7

Original Poster
Rep: Reputation: Disabled
Now it looks like this :
Code:
#!/bin/bash
argc=$#
host=$(hostname) 
if test  $argc -lt 0 ; #SPRAWDZAM CZY JEST JAKIS PARAMETR / PARAMETERS QUANTITY CHECK
then

	if test $1 -eq -u ; #SPRAWDZANIE CZY PODAJEMY USERA  / USER CHECK
 		then
		if test $3 lt 0; # SPRAWDZANIE CZY USEROW JEST WIECEJ NIZ 1 / CHECKING WHETHER THERE IS MORE THAN 1 USER
			then
			#OPERACJE DLA WIELU USEROW / OPERATIONS FOR MULTIPLE USERS
		else
			#OPERACJE DLA JEDNEGO USERA / OPERATIONS FOR SINGLE USERS
		fi
	else
	if test $1 -eq -g ; #SPRAWDZANIE CZY PODAJEMY GRUPY / GROUP CHECK 
		then
		if test $3 lt 0; # SPRAWDZANIE CZY GRUP JEST WIECEJ NIZ 1 / CHECKING WHETHER THERE IS MORE THAN 1 USER
		then
			#OPERACJE DLA WIELU GRUP / OPERATIONS FOR MULTIPLE GROUPS
		else
			#OPERACJE DLA JEDNEJ GRUPY / OPERATIONS FOR SINGLE GROUP
		fi
	else
	echo "WRONG FIRST PARAMETER"
	exit
	fi
	
fi



#OPERACJE DLA BEZ PARAMETRÓW
if test  $argc -eq 0 ;
	 then
	users="$(awk -F'[/:]' '{if ($3 >= 1000 && $3 != 65534) print $1}' /etc/passwd)"

	echo $users 

	for user in $users
		do
		FWOA_tmp.txt >> "######################$user#####################" && find / -type f -user $user -perm /333 2>/dev/null
	done
fi

echo "THIS IS FILE ACCESS RAPORT FROM $(date)" | mailx -s 'FILE ACCESS $(host)' -a FWOA_tmp.txt mail@domain.com
rm FWOA_tmp.txt
I made this divisions becouse i think that i won't be able to use the same instructions for single and multiple objects(users or groups). I'm right? I didn't take into account that user want to specify users which he want to check if they have access to his files.
format of command will be someting like sh script.sh (-u or -g) (multiply or single user or groups) -c(check) (users which he want to check if they have access to his files.) (mail address) it is ok?

Last edited by Justaguy123; 12-22-2017 at 08:44 AM.
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] BASH Script - What am I doing wrong in this test? - BASH Script BW-userx Programming 34 04-08-2017 01:36 PM
[SOLVED] Bash Script - Reading User Input while Processing output from Command within Bash cleeky Linux - General 5 05-27-2014 02:57 PM
[SOLVED] Converting Script from Linux (GNU) Bash 4 to Solaris Bash 2.05 - Any cheat sheet? oly_r Solaris / OpenSolaris 6 05-03-2013 08:25 AM
SSH connection from BASH script stops further BASH script commands tardis1 Linux - Newbie 3 12-06-2010 08:56 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:23 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
Open Source Consulting | Domain Registration