Script to check if file is owned by wasadmin

dear Sirs,

thank youuu so much. truly appreciate it.
sirs, how do you become expert in bash?
i have been listening to youtube video on bash.
this forum has been very helpful.
any recommended book or course to buy/attend.

thank youuusss

By frequently visiting this forum :slight_smile:

2 Likes

Dear Sirs
seeking advise.
when i list a directory (ls -lrt ) and compare with single uname2, it seem did not return accurate. the owner is wasadmin, but the script says it is not.

dirs=$(cat directory.txt)
server=$(cat server.txt)
while read -r "$server"
do
echo "$server ..."
ssh wasadmin@"$server" /bin/bash -s $dirs << "_EORS_"
# Remote Script
for dir
do
uname2=$(ls -lrt "$dirs" | awk '{print $3}')
if [ "$uname2" == "wasadmin" ];
then echo "wasadmin is owner of $dirs";
else echo "wasadmin is not owner of $dirs";
fi;
done
# End of Remote Script, do no indent the following marker
_EORS_
done < server.txt

thank yousss Sirs.

What is in $dirs?

dirs=$(cat directory.txt)
printf "%s\n" "$dirs"
exit
1 Like

Dear Sirs,
the content of dirs
/disc1/Log/
/disc5/logs/

thank yousss

I already posted the command substitution applicable to your use case.
You need to distinct between $dir and $dirs variable expansions, and understand what these variables store (and what the output would become when presenting them as arguments to ls.)

1 Like

Yes, you want "$dir" here.

Do you want the "$dir" itself?
Then it is
uname2=$( ls -ld "$dir" | awk '{print $3}' )
Or do you want the latest file in it?
Then it is
uname2=$( ls -ltr "$dir" | awk '{print $3}' | tail -1 )
or
uname2=$( ls -ltr "$dir" | awk '{u=$3}END{print u}' )
or
uname2=$( ls -lt "$dir" | awk '{print $3}' | head -1 )
or
uname2=$( ls -lt "$dir" | awk '{print $3;exit}' )

1 Like

Dear Sirs,
opss sorry sirs i missed the content.thanksss youss for pointing out.
sirs this script stil not working, can i improve along this line:

dirs=$(cat directory.txt)
server=$(cat server.txt)
while read -r "$server"
do
echo "$server ..."
ssh wasadmin@"$server" /bin/bash -s $dirs << "_EORS_"
for dir
do
ls -lrt "$dirs" | awk '{print $3}'
if [ "$uname2" == "wasadmin" ];
then echo "wasadmin is owner of $dirs";
else echo "wasadmin is not owner of $dirs";
fi;
done
_EORS_
done < server.txt
ssh wasadmin@"$server" /bin/bash -s $dirs << "_EORS_"
for dir
do
ls -ld "$dirs" | awk '{print $3}'
if [ "$uname2" == "wasadmin" ];
then echo "wasadmin is owner of $dirs";
else echo "wasadmin is not owner of $dirs";
fi;
done
_EORS_
done < server.txt

the directory is wasadmin, the output says not.
wasadmin
wasadmin is not owner of
wasadmin
wasadmin is not owner of
need advise ,why it did not return is owner of the directory.
thankss youss Sirs

Why are you still using $dirs as ls argument, after you've been told to use $dir?
What is the purpose of first/second "for loop"? (you only need one)
Why isn't uname2 variable assigned anywhere? (i.e. what are you comparing with wasadmin string?)

I still recommend learning the basics for understanding the code, before/instead of asking questions regarding random code modifications; Bash Tutorial | Bash Scripting Tutorial - Javatpoint looks quite ok (from the first look at it, although it has some issues later on)
BashGuide - Greg's Wiki looks (and is) much better, but offers yet a different document structure (not necessarily learning-basics-oriented).

1 Like

The for dir loops through the positional parameters that were given by $dirs; each cycle the loop variable is referred as $dir

The following runs the command in $( ) and assigns the result value to a variable:
uname2=$(ls -ld "$dir" | awk '{print $3}')
The variable is referred as $uname2

1 Like

Dear Sirs,
my apologies Sirs, thank youu so much , trully appreciate it.
Sir, i have updated the script. one of the command is to check the ownership of the files in a directory, when i did listing, the owner is wasadmin, but the script output says it is not.

dirs=$(cat directory.txt)
server=$(cat server.txt)
while read -r line;
do
echo "$line ..."
ssh wasadmin@"$line" /bin/bash -s $dirs << "_EORS_"
for dirs
do
uname2=$(ls -lrt "$dirs" | awk '{print $3}')
if [[ "$uname2" == "wasadmin" ]];
then echo -e "\e[34m wasadmin is owner of files in $dirs \e[0m";
else echo -e "\e[31m wasadmin is not owner of files in $dirs \e[0m";
fi;
uname3=$(ls -ld "$dirs" | awk '{print $3}')
if [ "$uname3" == "wasadmin" ];
then echo -e "\e[34m wasadmin is owner of directory $dirs \e[0m";
else echo -e "\e[31m wasadmin is not owner of directory $dirs \e[0m";
fi;
done
_EORS_
done < server.txt

the file listing in the directory:

total 102144
-rw-r--r-- 1 wasadmin wasgrp 43 Jul 24 2020 SystemOut.log.owner
-rw-r--r-- 1 wasadmin wasgrp 43 Jul 24 2020 SystemErr.log.owner
-rw-r--r-- 1 wasadmin wasgrp 12138 Aug 10 2020 serverStatus.log
-rw-r--r-- 1 wasadmin wasgrp 12150 Dec 23 2022 stopServer.log
-rw-r--r-- 1 wasadmin wasgrp 27050 Nov 27 2023 startServer.log
-rw-r--r-- 1 wasadmin wasgrp 6190383 Jun 20 12:27 verbosegc.010.log
-rw-r--r-- 1 wasadmin wasgrp 59391 Jun 20 12:35 verbosegc.001.log
-rw-r--r-- 1 wasadmin wasgrp 115158 Jun 20 18:02 verbosegc.002.log
-rw-r--r-- 1 wasadmin wasgrp 5126449 Jul 10 15:33 verbosegc.003.log
-rw-r--r-- 1 wasadmin wasgrp 1782349 Jul 15 10:32 verbosegc.004.log
-rw-r--r-- 1 wasadmin wasgrp 172989 Jul 15 23:36 verbosegc.005.log
-rw-r--r-- 1 wasadmin wasgrp 179134 Jul 16 09:28 verbosegc.006.log
-rw-r--r-- 1 wasadmin wasgrp 23114248 Aug 19 05:11 verbosegc.007.log
-rw-r--r-- 1 wasadmin wasgrp 10751405 Sep 02 09:25 verbosegc.008.log
-rw-r--r-- 1 wasadmin wasgrp 10344 Sep 02 15:06 native_stderr.log
-rw-r--r-- 1 wasadmin wasgrp 147560 Sep 02 15:06 native_stdout.log
-rw-r--r-- 1 wasadmin wasgrp 7 Sep 02 15:07 server1.pid
-rw-r--r-- 1 wasadmin wasgrp 112897 Sep 02 15:07 objects
-rw-r--r-- 1 wasadmin wasgrp 708 Sep 02 15:07 btrace.1

thank youssss Sirs

Your usage of variable names may (still) be confusing.

What do you think is generated by executing ls -lrt "$dirs" | awk '{print $3}'?
Hint: it's not a single line of text, it's multiple lines of text - for all files inside this directory.
So multiple lines containing e.g.

"
wasadmin
wasadmin
wasadmin"

cannot be simply compared to a single line, containing only one "wasadmin" string.
You'd need another loop inside (preferably). And yes, the first line is empty, because total ... is there in the output of ls -ltr, and it does not contain 3 fields for awk to parse correctly.

2 Likes
dirs=$(cat directory.txt)
# Loop over lines from server.txt
while read -r line
do
  echo "$line ..."
  # Run a remote /bin/bash with arguments $dirs and a script from stdin (standard input stream)
  ssh wasadmin@"$line" /bin/bash -s $dirs << "_EORS_"
# Remote Script starts here
  # Loop over arguments (from $dirs)
  for dir
  do
    # Loop over files
    for file in "$dir"/ "$dir"/*
    do
      uname2=$( ls -ld "$file" | awk '{print $3}' )
      if [[ "$uname2" == "wasadmin" ]]
      then echo -e "\e[34m wasadmin is owner of '$file' \e[0m"
      else echo -e "\e[31m wasadmin is not owner of '$file' \e[0m"
      fi
    done
  done
# End Of Remote Script
_EORS_
done < server.txt
2 Likes

Dear Sirs, thank youss so much for comments and guidance. truly apreciate it.
Sirs need great advise, though the file belong to wasadmin, my script did not return all as belong to wasadmin, it says some are not.
when i issue ls -ld /dir, all belong to wasadmin.

wasadmin is not owner of files in /websphere/IBM/WebSphere/AppServer/profiles/logs/server1
wasadmin is not owner of files in /websphere/IBM/WebSphere/AppServer/profiles/logs/server1
wasadmin is owner of files in /websphere/IBM/WebSphere/AppServer/profiles/logs/server1
wasadmin is owner of files in /websphere/IBM/WebSphere/AppServer/profiles/logs/server1
wasadmin is not owner of files in /websphere/IBM/WebSphere/AppServer/profiles/logs/server1

thank yousss Sirs

No, the output of ls -ld /dir only tells you, that /dir itself belongs to wasdamin, but it says nothing about the files inside.

Which version of the script are you currently using? (because to me it seems like you're still using the outdated one).
The code posted recently should give you a hint on how to solve for this, but only once you understand it and know how to adjust it to meet your needs.
Which files owned by wasadmin (according to you) are indicated as "not owned by wasadmin"? And how do you know exactly, which particular ones they are?
Asking the same question over and over, but just rephrasing it slightly, unlikely gets you anywhere closer to the desired functionality - this will change only if you start actually learning what the code does (and how the code does it).

1 Like

Dear Sirs,
Apologies, i forgotten to put the script earlier,

dirs=$(cat directory.txt)
# Loop over lines from server.txt
while read -r line
do
echo "$line ..."
# Run a remote /bin/bash with arguments $dirs and a script from stdin (standard                                                                                                              input stream)
ssh wasadmin@"$line" /bin/bash -s $dirs << "_EORS_"
# Remote script starts here
# Loop over arguments (from $dirs)
for dirs
do
# Loop over files
for file in "$dir/" "$dir"/*
do
uname2=$( ls -ld "$file" | awk '{print $3}' )
if [[ "$uname2" == "wasadmin" ]];
then echo -e "\e[34m wasadmin is owner of files in $dirs \e[0m";
else echo -e "\e[31m wasadmin is not owner of files in $dirs \e[0m";
fi;
done
done
# End of Remote Script
_EORS_
done < server.txt

Sirs, when i list the files inside the directory, there are all belong to wasadmin. but when i run it says some are not.

thank youssss

Carefully compare your script with the one posted earlier by MadeInGermany, line by line - as there's one substantial difference impacting its work (indentations and comments don't matter). After you do the comparison, answer these questions:

  1. Did you notice the difference?
  2. Do you understand, why this difference matters?
2 Likes

apologies, thank yousss so much Sirs,truly appreciate it.
it is because of dirs that is not defined in the remote script. truly appreciate it. thank yousss