dear friends,
i need help
i tried to run this script, encounter error.
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
dear friends,
thank youuuss
i tried to edit and run this script, i encounter error below.
$ 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 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
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.
#!/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$
#!/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
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:
uname2=$(ls -l /disc25/test | awk '{print $3}')
becomes
uname2=\$(ls -l /disc25/test | awk '{print \$3}')
as mentioned - the entire script also needs to be quoted
so
ssh userid@$server;
becomes
(get rid of the semi-colon; on the end of that line!)
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.
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:
#!/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.
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 ?)
Ugh... I know it works, and I know why it works, but "ew, uglee"
for dir in "$@"
do
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).
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
#!/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
--> 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:
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.
Looks like you're not using variables as intended (still need to learn some basics?).
It should rather be something similar to this:
#!/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, 2.
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.