# Script to check if file is owned by wasadmin

**URL:** <https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164>\
**Category:** AIX\
**Created:** [May 20, 2024, 3:24pm UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164 "2024-05-20T15:24:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 20, 2024, 3:24pm UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/1 "2024-05-20T15:24:12Z")

</div>

dear friends,  
i need help  
i tried to run this script, encounter error.

```plaintext
bash-4.3$ cat test3
#!/usr/bin/bash
$directory = /disc25/test
$uname2 = ls -l /disc25/test | awk '{print $3}'
if [$uname2 == wasadmin]
then echo "owner"
else echo "no owner"
fi
bash-4.3$

bash-4.3$ ./test3
./test3: line 2: =: command not found
./test3: line 3: =: command not found
./test3: line 4: [: missing `]'
no owner

```

thank youu

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [May 20, 2024, 3:32pm UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/2 "2024-05-20T15:32:36Z")

</div>

@apple08 , welcome.

for starters, no spaces in variable assignment

```plaintext
X=24 # ok
X =24 # not ok
X= 24 # not ok

```

check out the shellcheck utility, available via a Google search.....

> **[ShellCheck – shell script analysis tool](https://www.shellcheck.net/)**
>
> ShellCheck finds bugs in your shell scripts

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [May 20, 2024, 8:13pm UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/3 "2024-05-20T20:13:01Z")

</div>

assuming a linux type distro .... try something along the following:

```plaintext
OWNER=$(stat -c '%U' /disc25/test)
if ["$OWNER" == "wsadmin"]; then echo "owner : $OWNER"; else echo "no owner : $OWNER"; fi

```

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 21, 2024, 2:05am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/4 "2024-05-21T02:05:12Z")

</div>

dear friends,  
thank youuuss  
i tried to edit and run this script, i encounter error below.

```plaintext
$ cat test3
#!/usr/bin/bash
#directory=$(/disc25/test)
uname2=$(ls -l /disc25/test | awk '{print $3})'
if ["$uname2"=="wasadmin"];
then echo "owner";
else echo "no owner";
fi
$

$ ./test3
./test3: line 3: unexpected EOF while looking for matching `)'
./test3: line 8: syntax error: unexpected end of file
$

```

thank youusss

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 21, 2024, 2:05am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/5 "2024-05-21T02:05:59Z")

</div>

dear friends,  
im running the command in aix.

thank yousss

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [May 21, 2024, 3:56am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/6 "2024-05-21T03:56:19Z")

</div>

```plaintext
uname2=$(ls -l /disc25/test | awk '{print $3})'

```

Needs to be

```plaintext
uname2=$(ls -l /disc25/test | awk '{print $3}')

```

Delete the $ from the last line the test3 file

Did you even bother to use the shellcheck utility we advised ?

These are rudimentary coding errors, if you cannot be bothered to investigate and resolve these on your own you are going have a hard time improving.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [May 21, 2024, 5:23am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/7 "2024-05-21T05:23:41Z")

</div>

> [@apple08](#):
>
> `if ["$uname2"=="wasadmin"];`

Spaces missing!  
Change to  
`if ["$uname2" == "wasadmin"];`

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 23, 2024, 1:12am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/8 "2024-05-23T01:12:21Z")

</div>

thank youus so much sir. truly appreciate .  
sir sorry i tried the link, seem i could not understand, i will try in future script. thank youus so muchsir

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 23, 2024, 1:12am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/9 "2024-05-23T01:12:44Z")

</div>

thank youus so much sir. truly appreciate .

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 23, 2024, 2:42am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/10 "2024-05-23T02:42:48Z")

</div>

dear sirs,  
if i need to have 2 files to read in a script, how to do that ya..  
i ssh to servers and then read the directory file.  
when i tried to use the script, it stop after ssh and login to the server.

```plaintext
#!/usr/bin/bash
for server in $(cat server.txt);
do ssh userid@$server;
for line in $(cat directory.txt);
do
uname2=$(ls -l /disc25/test | awk '{print $3}')
if ["$uname2" == "wasadmin"];
then echo "wasadmin is owner of $line";
else echo "wasadmin is not owner of $line";
fi;
done
done
bash-4.3$

```

thank youuss sirs

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 23, 2024, 9:36am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/11 "2024-05-23T09:36:17Z")

</div>

> [@apple08](#):
>
> #!/usr/bin/bash  
> for server in $(cat server.txt);  
> do ssh userid@$server;  
> for line in $(cat directory.txt);  
> do  
> uname2=$(ls -l /disc25/test | awk '{print $3}')  
> if ["$uname2" == "wasadmin"];  
> then echo "wasadmin is owner of $line";  
> else echo "wasadmin is not owner of $line";  
> fi;  
> done  
> done

Dear Sirs  
i have made an update to the script:

```plaintext
#!/usr/bin/bash
for server in $(cat server.txt);
do ssh userid@$server;
if [$? eq 0]; then 
for line in $(cat directory.txt);
do
uname2=$(ls -l /disc25/test | awk '{print $3}')
if ["$uname2" == "wasadmin"];
then echo "wasadmin is owner of $line";
else echo "wasadmin is not owner of $line";
fi;
done
done

```

thank youuuss

---

<div class="post-metadata">

**Author:** ![munkeHoller](https://community.unix.com/user_avatar/community.unix.com/munkeholler/32/7431_2.png) [@munkeHoller](https://community.unix.com/u/munkeHoller)\
**Post date:** [May 23, 2024, 9:48am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/12 "2024-05-23T09:48:08Z")

</div>

@apple08 , is this homework ?

for every double quote " and dollar $ characters you will need to escape, that means putting a \ before each one. you also need to quote the entire script that will be executed on the remote host !

however, if **directory.txt** is on the controlling host you are going to have issues unless the remote hosts have the file **directory.txt** in the users login home directory. additionally, it makes no sense in the context of subsequent commands .... you are always looking for **/disk25/test**

example:

```plaintext
uname2=$(ls -l /disc25/test | awk '{print $3}')

```

becomes

```plaintext
uname2=\$(ls -l /disc25/test | awk '{print \$3}')

```

as mentioned - the entire script also needs to be quoted  
so

```plaintext
ssh userid@$server;

```

**becomes  
(get rid of the semi-colon; on the end of that line!**)

```plaintext
ssh userid@$server "
     for line in .....
      ...... 
     done
     "
done

```

my conclusion is that you are doing home/class work and hoping we'll  
do your debugging/testing for you. If you have colleagues go and ask them for help - build a relationship with experience devs/admins at your workplace, this will be more fruitful endevour.

NB: DO NOT change postings, post updates or start a new thread.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [May 23, 2024, 10:29am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/13 "2024-05-23T10:29:00Z")

</div>

`ssh userid@$server`  
without a further command argument becomes interactive. If you type `exit` then the following will run **locally**.

The following passes a quoted here document to a remote /bin/bash:

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

```

Because the remote script is in a "quoted here document" it does not need special quoting.

---

<div class="post-metadata">

**Author:** ![anon68137830](https://community.unix.com/letter_avatar/anon68137830/32/6_22734026db12803ebb3e059622ac3534.png) [@anon68137830](https://community.unix.com/u/anon68137830)\
**Post date:** [May 23, 2024, 1:19pm UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/14 "2024-05-23T13:19:19Z")

</div>

> [@MadeInGermany](#):
>
> ```plaintext
> dirs=$(cat directory.txt)
> for server in $(cat server.txt)
> 
> ```

```bash
dirs=$(<directory.txt)
for server in $(<server.txt)

```

Are all the entries (directories in `directory.txt` / servers in `server.txt`) delimited with spaces, tabs or newlines only?  
Also, are there any directories with names containing any space/tab/other white characters?  
(If yes, then why not use more reliable `while read -r` or even `mapfile -t` / `readarray -t` ?)

> [@MadeInGermany](#):
>
> ```plaintext
> for dir
> do
> 
> ```

Ugh... I know it works, and I know why it works, but "ew, uglee" 😃

```bash
      for dir in "$@"
      do

```

> [@MadeInGermany](#):
>
> ```plaintext
> uname2=$( ls -l "$dir" | awk '{print $3}' )
> 
> ```

And how do you guarantee, that `ls -l "$dir"` returns only one single line to stdout? What if it finds more files to display? (hint: then `$uname2` also contains multiple lines and comparing it to a single `wasadmin` becomes more problematic).

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [May 23, 2024, 1:40pm UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/15 "2024-05-23T13:40:25Z")

</div>

Ah yes, the `ls` lists the directory contents not the directory itself.  
Should be

```plaintext
uname2=$( ls -ld "$dir" | awk '{print $3}' )

```

or

```plaintext
uname2=$( ls -lod "$dir" | awk '{print $3}' )

```

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 24, 2024, 2:04am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/16 "2024-05-24T02:04:13Z")

</div>

Dear Sirs,  
thank yous sirs for great expertise.  
updated script, sirs, after i ran the script:  
i need to click enter for it to display the output.  
the script seem dont exit, after it finish first time, i click enter, it run same checking. sir how no need to enter and exit the script?  
it

```plaintext
#!/usr/bin/bash
dirs=$(cat directory.txt)
server=$(cat server.txt)
while read -r "$server"
do
echo "$server ..."
ssh db2fast@$server /bin/bash -s $dirs << "_EORS_"
# Remote Script
for dir
do
uname2=$(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
# End of Remote Script, do no indent the following marker
_EORS_
done

```

thank yousss sirs

---

<div class="post-metadata">

**Author:** ![apple08](https://community.unix.com/letter_avatar/apple08/32/6_22734026db12803ebb3e059622ac3534.png) [@apple08](https://community.unix.com/u/apple08)\
**Post date:** [May 24, 2024, 2:06am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/17 "2024-05-24T02:06:10Z")

</div>

--\> click enter  
server1 ...  
WARNING: By accessing and using this system you are consentingto system monitoring for law enforcement and other purposes. Unauthorized use of this computer system may subject you to criminal prosecution and penalties.  
test@server1's password:  
wasadmin is not owner of  
wasadmin is not owner of

----\> click enter  
server1 ...  
WARNING: By accessing and using this system you are consentingto system monitoring for law enforcement and other purposes. Unauthorized use of this computer system may subject you to criminal prosecution and penalties.  
test@server1's password:

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [May 24, 2024, 7:35am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/18 "2024-05-24T07:35:40Z")

</div>

The unquoted `$dirs` in the **main script** splits into arguments.  
In the **remote script** the  
`for dir`  
or  
`for dir in "$@"`  
loops through the arguments, where in each cycle `$dir` gets one argument.  
But you have `$dirs` that is undefined in the remote script. Please stick to my solution that has `$dir`

I don't know why you must hit Enter.  
If you have got ssh keys then it should not ask for a password and there is no need for Enter.

You can try `-q` option for `ssh` to suppress some diagnostic including the initial disclaimer.

---

<div class="post-metadata">

**Author:** ![anon68137830](https://community.unix.com/letter_avatar/anon68137830/32/6_22734026db12803ebb3e059622ac3534.png) [@anon68137830](https://community.unix.com/u/anon68137830)\
**Post date:** [May 24, 2024, 7:58am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/19 "2024-05-24T07:58:13Z")

</div>

> [@apple08](#):
>
> ```plaintext
> while read -r "$server"
> #[...]
> _EORS_
> done
> 
> ```

Looks like you're not using variables as intended (still need to learn some basics?).  
It should rather be something similar to this:

```bash
#!/usr/bin/env bash
dirs=$(<directory.txt)

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

```

although, I'd still tweak some things for `dirs=$(<directory.txt)`, `ssh db2fast@"$server" /bin/bash -s $dirs << "_EORS_"`, and `uname2=$(ls -ld "$dir" | awk '{print $3}')` due to some possible issues [1](https://www.shellcheck.net/wiki/SC2086), [2](https://www.shellcheck.net/wiki/SC2012).

E.g., as @munkeHoller mentioned earlier, `uname2=$(stat -c '%U' "$dir")` seems to be a better approach for a single file/directory.

And a general remark: don't apply any solutions from anyone blindly, if you don't fully understand how they work and how your code will behave after applying them - "please sir", learn the basics.

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_22734026db12803ebb3e059622ac3534.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [May 24, 2024, 9:55am UTC](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164/20 "2024-05-24T09:55:43Z")

</div>

Ah now I see it: the need for Enter comes from the while loop if its input is not redirected ( `done < server.txt` at the end)!

This is AIX forum, and AIX does not have a `stat` command, and bash is not "native", perhaps a bash 3.x

[Next page](https://community.unix.com/t/script-to-check-if-file-is-owned-by-wasadmin/394164.md?page=2)
