Beginner's Bash Scripting: "Unary Operator Expected" error && how to use grep to search for a variable from the output of a command? - linux

I'm attempting to write a bash script that is executed through "./filename username" and checks whether or not that user is logged in, printing the result. I'm still new to scripting and am having trouble understanding how to make this work.
I'm currently getting the error "line 7: [: ambonill: unary operator expected". What does that mean and how can I go about fixing that error?
Additionally, how would I get grep to work instead of sort | uniq? I'd like to grep for the variable from the output of the command but can't find anything related in the man page.
#! /bin/bash
# This script will take a username as an argument and determine whether they are logged on.
function loggedin {
for u in `who | cut -f1 -d" " | sort | uniq`
do
if [ $u == $1 ]
then
echo "$1 is logged on"
else
echo "$1 is not logged on"
fi
exit 0
done
}
loggedin $u
exit 1

Try to find a simpler solution, like:
#!/bin/bash
echo "$1 is $([ -z "$(w -h $1)" ]&&echo -n not\ )logged on"

Related

if condition for when a token is not found in a shell script

I am trying to construct an if-else block wherein one of the conditions is to echo a message if, when running a grep command on a text file, the specified token can not be found.
The grep command is
grep -i -n "token" file | cut -d':' -f 1
If the token is found, it will return the line number as usual. I want to know how to account for the case when the token does not exist in the text file and the terminal simply outputs nothing when the command is executed.
i.e.
if []
then
echo "This token does not exist in the file"
fi
I hope that's what you need:
result=$(grep -i -n "token" file | cut -d':' -f 1)
if [[ -z "$result" ]]; then
echo "Not found"
else
echo "$result"
fi

Brute force bash script with another bash script

so i currently have a bash script that takes a hash value and then asks the user to input a password, converts their input to hash and then compares.
#!/bin/bash
crypt="8277e0910d750195b448797616e091ad"
echo "please enter a password!"
read inc
hash="$((echo -n $inc|md5sum) | awk '{print $1}')"
if [[ "$hash" == "$crypt" ]];
then
echo "logged in"
else
echo "incorrect pass"
fi
I now want to create another program that brute forces this password by adding values(from a-z) into the password input but im running into trouble as i feel my knowledge on bash file manipulation is limited as ive never ran a script against another script before.
#!/bin/bash
for i in {a..z}; do
(echo -n "$i: " && ./hashscript $i) | grep logged in
done
Since the 1st script is reading the data from stdin (with read), the 2nd script will need to pass the data in that way:
#!/bin/bash
for i in {a..z}; do
(echo -n "$i: " && echo $i | ./hashscript) | grep logged in
done

Searching for a substring in a bash script will not work

I have been writing a bash script to call in my .bashrc file to print the results of whatis for a random command in my /usr/bin folder and wanted to exclude commands that returned "nothing appropriate" in the result and even if I use grep, wc, expr, ==, nothing seems to work. I have pretty much used every example here, and here with no progress. This is what I have so far but failes to do what I want when it finds somthing that contains "nothing appropriate." If anyone could figure out how to get it to work or what a good solution would be in this situation I would be greatfull.
#! /bin/bash
echo "Did you know that:";
while :
do
RESULT=$(whatis $(ls /usr/bin | shuf -n 1))
if [[ $RESULT != *"nothing appropriate"* ]]
then
echo $RESULT
break
fi
done
whatis prints the nothing appropriate message on the standard error stream. This stream is not caught by the $( ). This is the reason of your issue.
This is a way to fix it:
#! /bin/bash
echo "Did you know that:";
while :
do
RESULT=$(whatis $(ls /usr/bin | shuf -n 1) 2>&1 | cat - )
if [[ $RESULT != *"nothing appropriate"* ]]
then
echo $RESULT
break
fi
done
The 2>&1 | cat - addition does the trick

Bash : expression recursion level exceeded (error token is ...)

I'm writing a program that prints the username and the number of times that the user has logged in, or prints "Unknown user" otherwise.
My code is the following:
iden=$1
c='last | grep -w -c $iden'
if (( $c > 1 ))
then
echo "$iden $c"
else
echo "Unknown user"
fi
And I keep getting this error:
-bash: ((: last | grep -w -c 123: expression recursion level exceeded (error token is "c 123")
To store the output of a command in a variable you need to say var=$(command). Hence, use:
c=$(last | grep -w -c "$iden") # always good to quote the variables
instead of
c='last | grep -w -c $iden'
If you are learning Bash scripting, it is always handy to paste your code in ShellCheck to see the problems you may have.
You can use this too:
c=`last | grep -w -c "$iden"`

how to compare umask

I am trying to compare the umask of an user. I am getting an error while doing the comparison. The code I am using is
val=`su - user -c "umask" | tail -2 | sed -n "/[0-9]/p"`
if [ $val -eq 744 ]
then
echo "477 found."
fi
When I execute this I am geting an error like:
sh: ^[H: A test command parameter is not valid.
I've tried with = in the compare command, but it is still not working.
Please give any suggestions.
Regards.
val has been initialised as 0.
I am running this as root, so no login is there.
I've also tried giving quotes.
You should quote the variable name in your test expression:
if [ "$val" -eq 744 ]
See here for why.
Your code executes well on my machine, the only solution I can suggest is to use a slightly different syntax, sometimes different bash version complain about one syntax and accept another one:
val=`su - user -c "umask" | tail -2 | sed -n "/[0-9]/p"`
if [[ $val -eq 477 ]] ; then
echo "477 found."
fi
Have a look here for the difference between [ cond ] and [[ cond ]].

Resources