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.
Related
I'm doing a script to change the name of a user just using variables so the user doesn't see the actual command. I have done other things like change the folder of the user but for some reason tryng the same method with this isn't working, I hope you understand my mistakes and give me a hand.
echo "Give me the old username"
read name
echo "Give me the new username"
read new
echo "$new" | usermod -l --stdin "$name"
For some reason stdin isn't working :C
I'm getting the next output usermod: invalid user name '--stdin'.
Note:
I have used stdin to get the new names before and it worked perfectly just this way so I don't know what's wrong.
I don't know any usermod that knows --stdin ... try this:
echo "Give me the old username"
read name
echo "Give me the new username"
read new
usermod -l "$new" "$name"
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.
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).
one of the questions that I have been given to do for my Computer Science GCSE was:
Write a shell script that takes a string input from a user, asks for a file name and reports whether that string is present in the file.
However way I try to do it, I cannot create a shell script.
I don't need you to tell me the whole number, however, I have no idea where to start. I input the variable and the file name, however, I have no idea how to search for the chosen word in the chosen file. Any ideas?
Using grep can get this working, for example
viewEntry()
{
echo "Entering view entry"
echo -n "Enter Name: "
read input
if grep -q "$input" datafile
then
echo ""
echo -n "Information -> "
grep -w "$input" datafile
echo ""
else
echo "/!\Name Not Found/!\\"
fi
echo "Exiting view entry"
echo ""
}
dataFile is the file you would be reading from. Then making use of -q and -w arguments of grep, you should be able to navigate your chosen file.
This site does a great job explaining grep and your exact problem: http://www.cyberciti.biz/faq/howto-use-grep-command-in-linux-unix/
The following shell-script is a very quick approach to do what you suggested:
#!/bin/sh # Tell your shell with what program this script should be exectued
echo "Please enter the filename: "
read filename # read user input into variable filename
count=`grep -c $1 $filename` # store result of grep into variable count
if [ $count -gt 0 ] # check if count is greater than 0
then
echo "String is present:" $1
else
echo "String not found:" $1
fi
You should look at some tutorials to get the basics of shell-scripting. Your task isn't very complex, so after some reading you should be able understand what the script does and modify it according your needs.
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.