Made a script that requests user's correct password (qwerty) comparing a hash in text file. Referencing the script I then have to make a portfolio of items to choose from in other folders that load upon selecting 1-7 and 8 for exit. Have tried a lot given my comments but cannot get it working. Thanks.
#!/bin/bash
#ReadMe=`cat PasswordCheck.sh`
#echo "$ReadMe"
# https://stackoverflow.com/questions/7427262/how-to-read-a-file-into-a-variable-in-shell
#value=$(<PasswordCheck.sh)
#echo "$value"
bash PasswordCheck.sh
# https://stackoverflow.com/questions/13567947/run-bash-commands-from-txt-file
#read -sp 'PasswordCheck.sh'
Val_A="1. Create a folder"
Val_B="2. Copy a folder"
Val_C="3. Set a password"
Val_D="4. Calculator"
Val_E="5. Create Week Folders"
Val_F="6. Check Filenames"
Val_G="7. Download a File"
Val_H="8. Exit"
#read -sp 'UserPassword : ' Pass_Var
#PwdChk='PasswordCheck.sh'
#if read exit O "PasswordCheck.sh" exit 0 "$SecretHashedPassword"; then
if read "qwerty"; then
echo "1. Create a folder
2. Copy a folder
3. Set a password
4. Calculator
5. Create Week Folders
6. Check Filenames
7. Download a File
8. Exit"
exit 0
else
echo "Goodbye"
exit 1
fi
#case $Val_A in
#9) echo "Create a folder selected";;
#8|pork) echo "IDK...";;
#*) echo "Please select a response from numbers 1-4.";;
#esac
#"2. Copy a folder"
#"3. Set a password"
#exit 0
#https://www.linkedin.com/learning/search?entityType=VIDEO&keywords=bash%20case&u=2072140
#if ["$Val_A" -eq "1"]; then
#bash Foldermaker.sh
#if read "1"; then
#fi
exit 0
The password checking part of your script remains fuzzy to me, however I might be able to help you with the menu question.
Select might be what you need to get the menu of choices set up.
Below is a sceleton script that you can extend according to your needs:
#!/bin/bash
function menu() {
options=("Create_a_folder" "Copy_a_folder" "Set_a_password" "Calculator" "Create_Week_Folders" "Check_Filenames" "Download_a_File" "exit")
echo "Choose option:" 1>&2
select opt in ${options[#]}; do
case ${REPLY} in
*) choice="${options[$(( $REPLY - 1 ))]}"; break;;
esac
done
echo "$choice"
}
#passwordcheck=`somefunction`
passwordcheck=true # replace this once your function is ready
if [ "$passwordcheck" == "true" ]; then
choice=`menu`
[[ "$choice" == "exit" ]] && exit
echo "You have chosen: $choice"
fi
Related
So far I have been able to get my script to run, but have not been able to figure out how to get it to loop. Can anybody help me with this? I'll post what I have so far:
#!/bin/bash
#descripton
clear
echo "Please select a menu item"
echo
echo "1) list files in current directory"
echo "2) display block device layout of system"
echo "3) display last 10 lines of /var/log/messages"
echo "4) display RAM info"
echo "5) Display CPU info"
echo "6) exit the program"
echo
read CHOICE
case $CHOICE in
1) ls;;
2) lsblk;;
3) sudo tail -10 /var/log/messages;;
4) free -h;;
5) mpstat -u;;
6) exit;;
*) echo "you have made an invalid selection"
esac
I'm honestly just not sure what to do to get it to loop. Thanks in advance!
Tweaked your code a little bit. Put the printing of options and selecting of choice in separate functions. Added a loop to check non digits or choices greater than 6. It will continuously ask for choices until it an input from 1-6 is given.
#!/bin/bash
#descripton
print_options (){
echo "Please select a menu item"
echo
echo "1) list files in current directory"
echo "2) display block device layout of system"
echo "3) display last 10 lines of /var/log/messages"
echo "4) display RAM info"
echo "5) Display CPU info"
echo "6) exit the program"
echo
}
selection (){
case $CHOICE in
1) ls;;
2) lsblk;;
3) sudo tail -10 /var/log/messages;;
4) free -h;;
5) mpstat -u;;
6) exit;;
*) echo "you have made an invalid selection"
esac
}
print_options
read CHOICE
while [[ $CHOICE =~ [^0-9] || $CHOICE -gt 6 ]]
do
echo "Invalid choice"
print_options
read CHOICE
selection
done
I am trying to make this script handle file with spaces in them. it is supposed show and execute the content of files in a directory. when I select a file with a space in it, bash fails with bash: foo: no such file or directory, What am I missing to make this handle files correctly
# /etc/skel/.bashrc
#Interactive shell detection
if [[ $- != *i* ]] ; then
# Shell is non-interactive. Be done now!
return
fi
#kv-bash (easy) var database & setup of info
echo "type 'menu' for a bash menu"
#done####################
#to easily launch crouton enviroments
addentry() {
cd ~/.sslm
echo "Name your menu entry."
read entry
sleep 1s
if [ -e "$entry " ]
then
echo "Error, Menu entry already exists"
addentry
else
echo "what do you want the entry to do?"
read entryexec
echo "$entryexec && menu"> ~/.sslm/"$entry"
echo "done"
cd ~/
fi
sleep 1s
}
###################
delentry() {
cd ~/.sslm
ls -x
echo "what entry do you want to delete?"
read del
rm "$del"
echo "the work has been done, he is dead"
}
###################
menu() {
clear
cd ~/.sslm
echo "-- simple shell launcher menu v1.o --"
# set the prompt used by select, replacing "#?"
PS3="Use number to select a file or 'exit' to leave: "
# allow the user to choose a file
select filename in *
do
# leave the loop if the user says 'stop'
if [[ "$REPLY" == exit ]]; then
cd ~/
break
fi
# complain if no file was selected, and loop to ask again
if [[ "$filename" == "" ]]
then
echo "'$REPLY' is not a valid number"
sleep 1s
continue
fi
# now we can use the selected file, trying to get it to run the shell
script
. $filename
# it'll ask for another unless we leave the loop
break
done
}
menu
also, this is on a chromebook, so there is no apt.
At this part:
script
. $filename
I just needed to change to . "$filename"
thx #PesaThe
I'm trying to write a program where the program will ask you your name, confirm that you want this name, then check to see if you said yes or no. I have the "no" loop working, where it will ask for a name again, but I'm trying to a loop where if you type anything other than yes or no it will say "Please type yes or no." then ask if you want to confirm your name, then check the answer again. Heres what I have.
echo -e $WHITE"Name your$GREEN Hero$WHITE."
read HERO
clear
echo -e "Are you sure you want your$GREEN Hero$WHITE's name to be$GREEN "$HERO"$WHITE? y/n"
read ANSWER1
while [ $ANSWER1 = "no" ]; do
#
#
#
clear
echo -e $WHITE"Name your$GREEN Hero$WHITE."
read HERO
clear
echo -e "Are you sure you want your$GREEN Hero$WHITE's name to be$GREEN "$HERO"$WHITE? y/n"
read ANSWER1
done
while [ $ANSWER1 != "yes" ] || [ $ANSWER1 != "no" ]; do
#
#
#
clear
echo -e $WHITE"Please type yes or no."
sleep 1.5
clear
echo -e $WHITE"Are you sure you want your$GREEN Hero$WHITE's name to be$GREEN "$HERO"$WHITE?"
read ANSWER1
clear
done
clear
echo -e -n $WHITE"Loading"
sleep 1.5
echo -e -n "."
sleep 1.5
echo -e -n "."
sleep 1.5
echo -e -n "."
sleep 1.5
clear
echo -e "Done."
If I go through the "Please type yes or no." it will freeze at the sleep and not clear, therefore not displaying the echo and continuing the loop. Any help or suggestions would be greatly appreciated!
Here's the problem:
while [ $ANSWER1 != "yes" ] || [ $ANSWER1 != "no" ]; do
Every possible answer is different to at least one of them. There's no string that's equal to yes and no at the same time. You need && instead of ||.
An easier way to write a loop to get user's input is the select command:
while true; do
read -p "${white}Name your ${green}Hero$white: " hero
echo "Are you sure you want your ${green}Hero$white's name to be $green\"$hero\"$white? "
select ans in Yes No; do
case $ans in
Yes) break 2 ;; # break out of both select and while loops
No) break ;; # only break out of select loop
esac
done
done
echo "Your ${green}Hero$white's name is $green\"$hero\"$white."
I have been making script that will back up and restore a directory. I want to make it better but I need some help.
At the moment the I have the file being saved as just backup.tgz I did have the date added onto the end but when I ran the restore function the I could only have it look for the backup.tgz and not the backup with the date extension. Is there any way to have it look for the most recent backup? Or even look for the backup given by user input?
I have also tried to add a progress bar and make incremental back ups but had no luck there either if someone could help?
Tar Code
#!/bin/bash
ROOT="/Users/Rory/Documents"
ROOT_EXCLUDE="--exclude=/dev --exclude=/proc --exclude=/sys --exclude=/temp --exclude=/run --exlucde=/mnt --exlcude=/media --exlude=$
DESTIN="/Users/Rory/BackUps"
BACKUP="backup.tgz"
CREATE="/dev /proc /sys /temp /run /mnt /media "
if [ "$USER" != "root" ]; then
echo "You are not the root user"
echo "To use backup please use: sudo backup"
exit
fi
clear
echo "************************************************"
echo "********* Backup Menu **************************"
echo "************************************************"
OPTIONS="BACKUP RESTORE DESTINATION EXIT"
LIST="1)BACKUP 2)RESTORE 3)DESTINATION 4)EXIT"
select opt in $OPTIONS; do
if [ "$opt" = "EXIT" ]; then
echo "GOODBYE!"
sleep 3
clear
exit
elif [ "$opt" = "BACKUP" ]; then
echo "BACKING UP FILES..."
sleep 2
tar cvpfz $DESTIN/backup.tgz $ROOT $ROOT_EXCLUDE
echo "BACKUP COMPLETE"
sleep 2
clear
echo $LIST
elif [ "$opt" = "RESTORE" ]; then
echo "RESTOTING FILES..."
sleep 2
tar xvpfz $DESTIN/$BACKUP -C /Users/Rory/BackUps
sleep 2
if [[ -e "/proc" ]]; then
echo "$CREATE already exists! "
else
mkdir $CREATE
echo "$CREATE are created! "
fi
echo "RESTORE COMPLETE..."
clear
exit
elif [ "$opt" = "DESTINATION" ]; then
echo "CURRENT DESTINATION: $DESTIN/backup.tgz "
echo "TO CHANGE ENTER THE NEW DESTINATION..."
echo "TO LEAVE IT AS IS JUST PRESS ENTER..."
read NEW_DESTIN
#IF GREATER THEN 0 ASSIGN NEW DESTINATION
if [ ${#NEW_DESTIN} -gt 0 ]; then
DESTIN = "$NEW_DESTIN"
fi
clear
else
clear
echo "BAD INPUT!"
echo "ENTER 1 , 2, 3 or 4.."
echo $LIST
fi
done
Well, in the code snippet you posted, you are only looking for backup.tgz.
If you wanted to pick a specific one, you could modify your script to accept an argument and pick one based on a string you input. Or...if you wanted to do based on "how old", you could sort the backup files by date and allow the user to pick 0th, 1st, 2nd, etc.
One thing you may want to check out is rsync. Rsync can only copy files that have changed.
Plus, you can also enable a progress bar with rsync = )
rsync -avP /source/path/ /dest/path/
Check out the man page for more details man rsync
To enable progress bar on restore, you can untar the file and use rsync in reverse, and then you have progress updates = )
If you want to make this a custom numeric progress bar you'll probably need to do something more complicated than simply taking output from rsync.
I have a file and want to delete lines when user enters an ID in UNIX/Vi.
The file is called 'users' and contains:
001:joe:one:20:01:02
002:joe:two:21:06:02
003:joe:three:22:05:02
004:joe:four:23:04:02
I have used the following function in Vi:
function deleteRecord()
{
echo "Please enter staff ID: "
read userID
#store staffID in variable
sID=$( grep -w "$userID" users )
#store staff details only if user does not exist
#else prompt them to input again until they enter unused data
if [ $? -ne 0 ]
then
echo "Sorry that user does not exist!"
echo "Try entering a different staff ID to delete"
deleteRecord
elif [ $? -eq 0 ]
then
#:g/^$userID:/d
#sed /$userID/d users >tmp
#imv tmp users
echo "You have successfully deleted the user."
sleep 2
mainMenu
fi
}
I have tried that but it does not work! Is sed the problem?
Please help.
You're best off using the built in functionality. e.g.
:g/^theuserid:/d
Only for fun - here is your homework. Next time tag it.
#!/bin/bash
IDFILE="./testfile"
function killVogon() {
while read -p 'Enter Vogon-ID (or "q" for return) >' id
do
case "$id" in
(q|Q) return;;
esac
grep "^0*$id:" "$IDFILE" >/dev/null 2>&1
case $? in
(0) ;;
(1) echo "Although it is infinitely improbable, the '$id' does not exists!"; continue;;
(*) echo "You're blind and can't see any Vogons around"; sleep 1 ; return;;
esac
sed -i '.bak' -e "/^0*$id:/d" "$IDFILE"
case $? in
(0) echo "The Vogon is successfully killed"; sleep 1; return;;
(*) echo "Huh... Marvin, powered with pangalactic gargleblaster is too paranoid now. The Vogon is NOT killed" ;;
esac
done
}
killVogon