I have a bash script and tried very hard but i couldn't solve it please help

please can you help me with this script ( very very important )

what I'm trying is to write program that accepts list of user as its argument
1- If a user or more are given as arguments, the script should reset files permissions as follows:
a. Directory ~/share to 750 (if it exists).
b. All regular files inside ~/share to 744.
c. All directories inside ~/share to 750.
d. All other regular files in ~ to 600 and all other directories in ~ to 700.
e. files of other types are left as they are.

We assume that there is only one level of files and directories inside ~ (except for ~/share), and there is only one level of files and directories inside ~/share.

2- If no arguments are give, the script should identify normal users in the system (UID >= 500) and for each user, reset files permissions as described above.

3- At the end of the execution, the script must produce a file named report.txt that contains a list of all files in the user's home directory including full path, owner, group and permissions for each file.

please help me if you can

Look very much homework...

What have you done so far?

I am very new to Linux and this is what some friend of mine give me to test what I learn
and i tried very much to solve it but till now I couldn't do anything

All right, what have you tried then?

Start with "man find" especially -type and -path, but not -exec.

Something like this is very durable and fast, one line for each situation:

find ... -type d | xargs -n 101 chmod 765
[ -d ~/share ] && find ~/share -type f -print0 | while read -d $'\0' file
do
  # if $file exists and is a regular file
  [ -f "$file" ] && chmod 744 "$file"
done

Easy to match to spec and comment (case is nice that way, but use balanced parentheses so vi % is not disabled):
(Note: files in ~ like .profile, code, scripts no longer executable)

find ~ |while read p
do
 case "$p" in
 (~/share)
   chmod 750 "$p"
   ;;
 (~/share/*)
   if [ -f "$p" ];then
     chmod 744 "$p"
   else
     if [ -d "$p" ];then
       chmod 750 "$p"
     fi
   fi
   ;;
 (*)
    if [ -f "$p" ];then
     chmod 600 "$p"
   else
     if [ -d "$p" ];then
       chmod 700 "$p"
     fi
   fi
   ;;
 esac
done
1 Like

and what about the second one
2- If no arguments are give, the script should identify normal users in the system (UID >= 500) and for each user, reset files permissions as described above.

---------- Post updated at 01:11 PM ---------- Previous update was at 12:17 PM ----------

and what about the second one
2- If no arguments are give, the script should identify normal users in the system (UID >= 500) and for each user, reset files permissions as described above.

I would do that in a wrapper, and call the separately tested child script from it. It simplifies testing of both. If you want, later one can be a function within the other.

cut, sed or awk could find the /etc/passwd fields you need, and selectively invoke the script. Somewhere, ensure the home dir exists or is gracefully logged and ignored!

sed '
  s/^[^:]*:[^:]*:\([^:]*\):[^:]*:[^:]*:\([^:]*\):.*/\1 \2/
  /^[0-9]\{0,2\} /d
  /*[0-4][0-9][0-9] /d
  / \/$/d
  / $/d
  s/^/your_script /
 ' /etc/passwd >all_script

Narrative: Pull fields 3 and 6, colon : separated, from /etc/passwd, now separated by a space; if 3, the user #, is 0 to 2 digits, or if it is three digits starting 0-4, it is too low. If the nominal home dir is '/', we don't want to do this. If there is no home dir, we don't want to try. Stick your script on the front of every line.

L will try it now .

Enjoy, but be careful!