How do I search for a certain piece of text inside of a variable? - linux

I am working on a script which prompts the user for their username. Once entered, the script uses the 'rwho' command to get a list of users who are logged into the network. It should crosscheck the text they entered (their username) with the results from the rwho command.
If a match is found then it displays a message saying so, if not then it also makes the user aware of this.
Here is the script and my attempt so far:
#!/bin/sh
#
# User network checking script
#
# Using rwho command to get user list
OUTPUT="$(rwho)"
echo "${OUTPUT}"
# Prompt for username
echo "Please enter your username: "
read username
# Input validation
if [ -z "$username"]
then
echo "No username supplied"
echo "Please enter your username: "
read username
fi
# Search for user
if `echo ${OUTPUT} | grep "${username}" 1>/dev/null 2>&1'
then
echo "$username is logged in."
else
echo "$username is not present."
fi
I consistently get errors with the Search for User part. I don't have outstanding knowledge of Linux so if anyone could fix this and help me I would be greatly appreciative.

Your usage of quotes is weird.
if echo "$OUTPUT" | grep -q "$username"
should work.
-q makes grep quiet (and is shorter than your redirections).

Related

For each parameters, script checks if there exists a user of such username. If exists script prints all processes run by this user

#!/bin/bash
if [ $# -ne 1 ]
then
echo "Script Should Have Atleast 1 Parameter"
exit
fi
USER=$1
echo $USER
if [ $? -eq 0 ]
then
echo " Yes the User Exists"
else
echo "No , The User Doesnt Exists"
fi
My problem here is whatever the input i give, it shows yes the user exists.
And can anyone suggest me a command to print out all the process runs by this user
Should i use awk , or grep
Well, first of all, you are not checking if user exists, but ultimately if command echo $USER didn't fail. You will need to implement proper user check.
Good way how to check if arbitrary user exists would be something like this, using command id.
if id "${1}" &> /dev/null; then
echo 'user found'
else
echo 'user not found'
fi
Next you want to list all processes belonging to this user. You will need ps. There are many ways how ps can format output. Be sure to check manual page of ps. Here is example which lists command and pid of all processes belonging to user in "$1".
ps -eo pid,comm --user $(id -u "${1}")
At last, running check using id and only then using ps for process list is inefficient at least. In your use-case, you can simply run ps and check if it was successful. If not, you can show warning that user was not found.
#!/bin/bash
if ! ps -u "$1" 2> /dev/null; then
echo 'user not exists!'
exit 1
fi

How do -s and -p alter the read command?

I'm trying to interpret this block of code. Searched google to see what these commands mean and no luck. I put my interpretation of what each line/block means to me. If I am wrong, please correct me. I am new to unix commands. Code:
#!/bin/bash
# input 1st command line argument for the version.
export VERSION=$1
# if user didn't input a version, print the echo message and exit (not sure what -n means but I am assuming)
if [[ ! -n "$VERSION" ]]; then
echo "Missing Version"
exit 1
fi
# creating variable UNAME that tells who the person is (their name)
export UNAME='whoami'
# no idea what -s and -p mean but i think this prints the message "enter password for $UNAME" and stores it in a new variable named PASSWORD. the $UNAME will print whatever whoami said.
read -s -p "Enter password for $UNAME: " PASSWORD
echo ""
The -p flag issues a prompt before reading input into a variable
The -s flag stop the typed response from being shown (i.e. for a sensitive password)
More information is available here:
https://linuxhint.com/bash_read_command/
-p
prompt output the string PROMPT without a trailing newline before
attempting to read.
-s
do not echo input coming from a terminal.

How to create a bash script in Linux that checks if the user is local or not

I'm trying to create a Linux bash script that prompts for a username. For example, it asks for a username, once the username it's typed it will check if the user exists or not. I already tried to do it, but I'm not sure if I did it correctly.
I would appreciate your help.
Here is how I did it:
#!/bin/bash
echo "Enter your username:"
read username
if [ $(getent passwd $username) ] ; then
echo "The user $username is a local user."
else
echo "The user $username is not a local user."
fi
Try the following script :
user="bob"
if cut -d: -f1 /etc/passwd | grep -w "$user"; then
echo "user $user found"
else
echo "user $user not found"
fi
The file /etc/passwd contains a list of the local users along with some parameters for them. We use cut -d: -f1 to only extract the usernames, and match it with our user with grep -w $user. The if condition evaluate the exit code of the function to determine if the user is present.
if id "$username" >/dev/null 2>&1; then
echo "yes the user '$username' exists"
fi
OR
getent command is designed to gather entries for the databases that can be backed by /etc files and various remote services like LDAP, AD, NIS/Yellow Pages, DNS and the likes.
if getent passwd "$username" > /dev/null 2>&1; then
echo "yes the user '$username' exists"
fi
Will do your job, for example below
#!/bin/bash
echo "Enter your username:"
read username
if getent passwd "$username" > /dev/null 2>&1; then
echo "yes the user '$username' exists"
else
echo "No, the user '$username' does not exist"
fi
Try this.
#!/bin/sh
USER="userid"
if id $USER > /dev/null 2>&1; then
echo "user exist!"
else
echo "user deosn't exist"
fi

Something wrong with my sh script dont know why?

I wrote this sh script here. What it suppose to be doing is it prompts the user to type in the old password, then checks the password with the password in the "PASSWORD.txt" file if not it would exit, else if it matches then it would ask the user to type in the new password twice. Then it checks if the two new passwords are the same if not it would exit, else i should put the input the user typed and replace the text in the "PASSWORD.txt" file.
Then when i ran the file where it askes me for the old password i got this error:
Please Enter teh old passsword:
test
cat: .txt: No such file or directory
The password doesn't match![root#guzzy ~]#
The thing is the input i typed doesn't match even though i typed the correct old password.
Here is the scirpt below:
#!/bin/sh
clear
echo -e "Please Enter the old password:"
read old
if [ "$old" != "$(cat $PASSWORD.txt)" ]
then
echo -n "The password doesn't match!"
exit
else
echo -n "The old password matches!"
echo -n "Please Enter New password:"
read new1
echo -n "Please Enter New password again:"
read new2
if [ "$new1" != "$new2" ]
then
echo -n "The new passwords don't match!"
exit
else
$new1 >> PASSWORD.txt
echo -n "The new password has been saved!"
fi
fi
Please help thanks!
This line:
$new1 >> PASSWORD.txt
should be like this:
echo "$new1" > PASSWORD.txt
You need to echo the value into the file. I'm assuming that you don't want to keep old values. In order to be able to run your script again on the same file, you should probably overwrite (>) rather than append (>>).
You haven't set the PASSWORD variable somewhere. If your file is named 'PASSWORD.txt', remove the $ before it.
It think you meant
if [ "$old" != "$(cat PASSWORD.txt)" ]
without the dollar sign.

Getting user to authenticate the password

I'm trying to write a sh coding to get the user to authenticate the password by comparing the user input to the first 32 characters of a file. So basically if the password is correct it would run TaskMenu.csh if its wrong the program would exit.
#!/bin/sh
clear
echo -e " Please Enter the Password to access the TaskMenu:"
read PW
if (! -e "$PASSWORD.txt")
then
echo -n "The file doesn't exist"
echo kil
exit
else
...(i have no clue what to do)...
Please help
if [ "$PW" = $(cat "$PASSWORD.txt | head -c 32) ]
then
./TaskMenu.csh
else
echo Authentication failed.
exit 3
fi
Run with bash -x, or add set -x to the top of your source to see what strings are being passed around.

Resources