Need help with permissions in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with permissions in bash
# 1  
Old 09-15-2010
Need help with permissions in bash

I've set up a little script that will read from a text file. So here goes: I put 3 users in the text file, matt, luke, and rick. I created a group called members and added all 3 users to that group. What I need to know is how do I set permissions for each user to access their own files and directories and not each others. Last, I need another user (jones) from the outside that's not in the group to be able to read all 3 users files and directories, but the users cannot view his. The code I have is this:
#!/bin/bash

CURRENTUSERS=`awk -F":" '{ print $1 }' /etc/passwd`
EXPECTEDUSERS=`cat users.txt`
USERFOUND=0
for i in $EXPECTEDUSERS ; do
for x in $CURRENTUSERS ; do
if [ "$x" = "$i" ] ; then
USERFOUND=1
fi
done
if [ $USERFOUND -eq 1 ] ; then
echo Found user: $i
USERFOUND=0
else
useradd -m /home/$i
echo Adding user: $i
sed s/$i:.*:.*:.*:.*:.*:.*:.*:/$i:XOHWcSkDQKZaA:14208:0:99999:7:::/g </etc/shadow>/etc/shadow2
cp /etc/shadow2 /etc/shadow
rm /etc/shadow2
fi

done
exit 0
Let me know if I'm missing anything or if this is right or not. Thanks

Last edited by mrflipr; 09-15-2010 at 10:20 AM.. Reason: left out info
# 2  
Old 09-15-2010
Quote:
Originally Posted by mrflipr
I've set up a little script that will read from a text file. So here goes: I put 3 users in the text file, matt, luke, and rick. I created a group called members and added all 3 users to that group. What I need to know is how do I set permissions for each user to access their own files and directories and not each others. Last, I need another user (jones) from the outside that's not in the group to be able to read all 3 users files and directories, but the users cannot view his. The code I have is this:
Code:
 
#!/bin/bash
 
CURRENTUSERS=`awk -F":" '{ print $1 }' /etc/passwd`
EXPECTEDUSERS=`cat users.txt`
USERFOUND=0
for i in $EXPECTEDUSERS ; do
for x in $CURRENTUSERS ; do
if [ "$x" = "$i" ] ; then
USERFOUND=1
fi
done
if [ $USERFOUND -eq 1 ] ; then
echo Found user: $i
USERFOUND=0
else
useradd -m /home/$i
echo Adding user: $i
sed s/$i:.*:.*:.*:.*:.*:.*:.*:/$i:XOHWcSkDQKZaA:14208:0:99999:7:::/g </etc/shadow>/etc/shadow2
cp /etc/shadow2 /etc/shadow
rm /etc/shadow2
fi
 
done
exit 0

Let me know if I'm missing anything or if this is right or not. Thanks


---------- Post updated at 08:34 AM ---------- Previous update was at 08:31 AM ----------

you can give permissions like this :--
Code:
the 100's are for the owner of the file
400 reed
200 write
100 execute

10's are for the group of the file
40 read
20 write
10 execute

1's are for everyone else
4 read
2 write
1 execute



---------- Post updated at 08:35 AM ---------- Previous update was at 08:34 AM ----------

below are the commands that might help you :-

Code:
chmod /*change the ppermissions of a file*/
chown /*change the owner of the file*/
chgrp /*change the group that the file belongs to*/

# 3  
Old 09-15-2010
Whats the best way?

Quote:
Originally Posted by dazdseg


---------- Post updated at 08:34 AM ---------- Previous update was at 08:31 AM ----------

you can give permissions like this :--
Code:
the 100's are for the owner of the file
400 reed
200 write
100 execute

10's are for the group of the file
40 read
20 write
10 execute

1's are for everyone else
4 read
2 write
1 execute



---------- Post updated at 08:35 AM ---------- Previous update was at 08:34 AM ----------

below are the commands that might help you :-

Code:
chmod /*change the ppermissions of a file*/
chown /*change the owner of the file*/
chgrp /*change the group that the file belongs to*/

What would be easier to do, put the commands in the script or run it in putty? Also, does file refer to directories too? I know I need to use the chmod, what's confusing me is what to do with it to specify something. I would understand the concept of a text file with chmod, but not the directories.

Last edited by mrflipr; 09-15-2010 at 10:57 AM.. Reason: more info
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

2. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

3. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

4. Shell Programming and Scripting

Bash to select text and apply it to a selected file in bash

In the bash below I am asking the user for a panel and reading that into bed. Then asking the user for a file and reading that into file1.Is the grep in bold the correct way to apply the selected panel to the file? I am getting a syntax error. Thank you :) ... (4 Replies)
Discussion started by: cmccabe
4 Replies

5. UNIX for Dummies Questions & Answers

Im new to bash scriping and i found this expression on a bash script what does this mean.

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi espeacailly the top regex part? ---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
Discussion started by: kevin298
1 Replies

6. Shell Programming and Scripting

ksh; Change file permissions, update file, change permissions back?

Hi, I am creating a ksh script to search for a string of text inside files within a directory tree. Some of these file are going to be read/execute only. I know to use chmod to change the permissions of the file, but I want to preserve the original permissions after writing to the file. How can I... (3 Replies)
Discussion started by: right_coaster
3 Replies

7. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Shell Programming and Scripting

bash and ksh: variable lost in loop in bash?

Hi, I use AIX (ksh) and Linux (bash) servers. I'm trying to do scripts to will run in both ksh and bash, and most of the time it works. But this time I don't get it in bash (I'm more familar in ksh). The goal of my script if to read a "config file" (like "ini" file), and make various report.... (2 Replies)
Discussion started by: estienne
2 Replies

9. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

10. HP-UX

To give the "unzip" permissions & "create" file permissions

Hi, I am a Unix Admin. I have to give the permissions to a user for creating new file in a directory in HP-Ux 11.11 system since he cannot able to create a new file in the directory. Thanks in advance. Mike (3 Replies)
Discussion started by: Mike1234
3 Replies
Login or Register to Ask a Question