Bash while loop - bash

I'm trying to make a while loop.
This loop should either echo "try again or type exit to quit" when typing gibberish or finger a user if typed the user name.
echo Please enter a user name to find.
read username
done_fn()
{
finger $username
exit 0
}
continue_fn()
{
echo 'try again or type exit to quit'
read exitvar
}
grep $username /etc/passwd >/dev/null
while [ $? -eq 0 ]
do
done_fn
done
exitvar=quit
until [ $exitvar = exit ]
do
continue_fn
done
while [ -u $exitvar ]
do
done_fn
done
This is everything I have got so far. Whenever I I type in a username after 'try again or type exit to quit' it will just echo it again. I would like it to finger the user instead of echoing it again. I have looked in so many places and can't find the answer.
Thank you in advance.

You can try this:
typeset u="John Doe"
while ! grep -q "^$u:" /etc/passwd
do
echo -n "username? "
read u
if [[ $u = "q" ]]
then
exit
fi
done
finger $u

echo Please enter a user name to find.
read username
donev2_fn()
{
finger $exitvar
exit 0
}
done_fn()
{
finger $username
exit 0
}
continue_fn()
{
echo 'try again or type exit to quit'
read exitvar
grep $exitvar /etc/passwd >/dev/null
if [ $? -eq 0 ]
then
donev2_fn
fi
}
grep $username /etc/passwd >/dev/null
while [ $? -eq 0 ]
do
done_fn
done
exitvar=quit
until [ $exitvar = exit ]
do
continue_fn
done
I figured it out. I had to put an extra function in it. Thank you for all the help.

Related

Repeat an IF statement until condition is met [duplicate]

I have a script and want to ask the user for some information, but the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to achieve this but it doesn't work for some reason:
echo "Please change password"
while passwd
do
echo "Try again"
done
I have tried many variations of the while loop:
while `passwd`
while [[ "`passwd`" -gt 0 ]]
while [ `passwd` -ne 0 ]]
# ... And much more
But I can't seem to get it to work.
until passwd
do
echo "Try again"
done
or
while ! passwd
do
echo "Try again"
done
To elaborate on #Marc B's answer,
$ passwd
$ while [ $? -ne 0 ]; do !!; done
Is nice way of doing the same thing that's not command specific.
You need to test $? instead, which is the exit status of the previous command. passwd exits with 0 if everything worked ok, and non-zero if the passwd change failed (wrong password, password mismatch, etc...)
passwd
while [ $? -ne 0 ]; do
passwd
done
With your backtick version, you're comparing passwd's output, which would be stuff like Enter password and confirm password and the like.
If anyone looking to have retry limit:
max_retry=5
counter=0
until $command
do
sleep 1
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
echo "Trying again. Try #$counter"
((counter++))
done
You can use an infinite loop to achieve this:
while true
do
read -p "Enter password" passwd
case "$passwd" in
<some good condition> ) break;;
esac
done
while [ -n $(passwd) ]; do
echo "Try again";
done;

bash scripting one line shortcut for if test

Is there a shortcut (ie all on one line) for doing the following?
if [ -z "$PASSWORD" ] ; then
echo "PASSWORD envvar required. Exiting..."
exit 1
fi
I'm thinking along the lines of the following. Or maybe there's an even shorter way.
[ -z "$PASSWORD" ] && ...
That is, how do put the echo then exit after the &&
You can do it like this:
[ -z "$PASSWORD" ] && { echo "PASSWORD envvar required. Exiting..."; exit 1; }
Note the extra ; at the end of the line before the }, this is necessary and you cannot leave it out.
It's called a command group.
[[ -z "$PASSWORD" ]] && echo "PASSWORD envvar required. Exiting." && exit 1
Your single brackets will work fine. Consider learning to use the doubles, though.

Try/Except loop in shell [duplicate]

I have a script and want to ask the user for some information, but the script cannot continue until the user fills in this information. The following is my attempt at putting a command into a loop to achieve this but it doesn't work for some reason:
echo "Please change password"
while passwd
do
echo "Try again"
done
I have tried many variations of the while loop:
while `passwd`
while [[ "`passwd`" -gt 0 ]]
while [ `passwd` -ne 0 ]]
# ... And much more
But I can't seem to get it to work.
until passwd
do
echo "Try again"
done
or
while ! passwd
do
echo "Try again"
done
To elaborate on #Marc B's answer,
$ passwd
$ while [ $? -ne 0 ]; do !!; done
Is nice way of doing the same thing that's not command specific.
You need to test $? instead, which is the exit status of the previous command. passwd exits with 0 if everything worked ok, and non-zero if the passwd change failed (wrong password, password mismatch, etc...)
passwd
while [ $? -ne 0 ]; do
passwd
done
With your backtick version, you're comparing passwd's output, which would be stuff like Enter password and confirm password and the like.
If anyone looking to have retry limit:
max_retry=5
counter=0
until $command
do
sleep 1
[[ counter -eq $max_retry ]] && echo "Failed!" && exit 1
echo "Trying again. Try #$counter"
((counter++))
done
You can use an infinite loop to achieve this:
while true
do
read -p "Enter password" passwd
case "$passwd" in
<some good condition> ) break;;
esac
done
while [ -n $(passwd) ]; do
echo "Try again";
done;

Shell script to specify a user is logged into computer

username=$1
freq=$2
checkuser()
{
if who grep "$1"
then
sleep 60
fi
}
if [ -n "$1" ]
then
echo "Enter username"
read username
checkuser
echo -e "$1 is logged on \a"
echo -e "$1 logged in at `date`">>LOG
checkuser
else
echo "User is not logged on"
fi
I need to integrate a second argument into my code which allows for the user to specify after what time should the script check to see who is logged in. I have it set to 60 seconds currently and this needs to be the default frequency. I tried to use another function but to no avail. I thought of something like this...
if [ "$2" -ne 0 ]
then
freq=$2
else
freq=60
Thanks William for that was very helpful!! I changed the code a bit and came up with this. I now need to add a 3rd argument "X" which when selected just sends a message to the LOGFILE and not to the screen. I made an attempt but not doing as intended.
username=$1
freq=${2:-10}
X=$3
checkuser()
{
whoami|grep "$1";
}
while checkuser "$username"
do
echo -e "$1 is logged on \a"
echo "$1 logged in at `date`">>LOGFILE
sleep $freq
exit 0
done
echo "User is not logged in"
if [ "$3" -ne 1 ]
then
echo "$1 logged in at `date`"LOGFILE
fi
username=$1
freq=${2:-60} # Set a default frequency
checkuser(){ who | grep -q "$1"; }
while ! checkuser "$username"; do
echo "User is not logged on"
sleep $freq
done
echo "$1 is logged on"
Also note that you can simplify the setting of username:
username=${1:-$( echo "Enter username: "; read u; echo $u; )}
echo 'Enter id'
read id
res=`who | grep "$id" | wc -l`
if [ $res -eq 0 ]
then
echo 'user is not logged in'
else
echo 'user is logged in'
fi
A simple shell script that takes the username as input and determines whether the user is currently logged in.
if [[ $# -eq 0 ]]; then
echo "Usage: " $0 "username"
exit 1
fi
result="$(who | grep $1 | wc -l)"
if [[ $result -gt 0 ]]; then
echo "$1 is currently logged in"
else
echo "$1 is not logged in"
fi
exit 0

Bash Function Components Working Independently but Not Together

I have a bash script that has this function in it:
function start_vi()
{
echo "Please enter a file name with complete path to open in vi:"
read input_file
if [ -d "$input_file" ]
then
echo "You entered a directory."
echo "Please try again and enter a readable/writable file."
fi
grep_var="file $input_file | grep -c data"
if [ $? -eq 0 ]
then
vi $input_file
else
echo "File not found or invalid file type. Please try again."
fi
}
For the most part the function works fine. My problem is that the two if statements work fine independently, eg, if I comment out one of them, the test works and it does what I want. But together, as written, for example, when I type in a directory at the prompt, vi opens it as a file, where the test should return an echo saying that it's a directory, as it does when functioning alone.
Any ideas on why this is? I'm still relatively new at bash scripting so it is probably easy for the pros, but I have been banging my head against the wall for a while now.
Thanks in advance.
Add a return statement in the first if/then:
function start_vi()
{
echo "Please enter a file name with complete path to open in vi:"
read input_file
if [ -d "$input_file" ]
then
echo "You entered a directory."
echo "Please try again and enter a readable/writable file."
return
fi
grep_var="file $input_file | grep -c data"
if [ $? -eq 0 ]
then
vi $input_file
else
echo "File not found or invalid file type. Please try again."
fi
}
Otherwise, it will print and then open the file anyway, as your second test should be like this:
file $input_file | grep -c data
if [ $? -eq 0 ]
The $? is the exit code of the last run command. Assigning to a variable (i.e. grep_var="...") sets $? to 0. What you seem to have wanted is the exit code of grep -c data - in that case, use backticks ` instead of quotes " to run commands, like below. Or you can write it like this:
grep_var=`file $input_file | grep -c data`
if [ $grep_var != 0 ]
to compare the string value (i.e. what grep -c data returns - the count of data lines).
Doing some of the above should solve the problem.
You need a loop
function start_vi()
{
echo "Please enter a file name with complete path to open in vi:"
read input_file
while [ -d "$input_file" ]
do
echo "You entered a directory."
echo "Please try again and enter a readable/writable file."
read input_file
done
grep_var="file $input_file | grep -c data"
if [ $? -eq 0 ]
then
vi $input_file
else
echo "File not found or invalid file type. Please try again."
fi
}
All you need is a loop:
....
read input_file
while [ ! -f "$input_file" ]
do
echo "You did not enter a file"
echo "Please try again and enter a readable/writable file."
read input_file
done
grep_var="file $input_file | grep -c data"
if [ $? -eq 0 ]
then
vi $input_file
else
echo "File not found or invalid file type. Please try again."
fi
I think this may be closer to what you want to do..
function start_vi()
{
echo "Please enter a file name with complete path to open in vi:"
read input_file
grep_var=`file $input_file 2>&1 | grep -c data`
while [ $? -ne 0 ]
do
echo "File not found or invalid file type. Please try again."
read input_file
grep_var=`file $input_file 2>&1 | grep -c data`
done
vi $input_file
}

Resources