vendredi 21 juin 2019

How to Ask User for Confirmation

I am new to shell, and my code takes two arguments from the user. I would like to confirm their arguments before running the rest of the code. I would like a y for yes to prompt the code, and if they type n, then the code will ask again for new arguments

Pretty much, if i type anything when I am asked to confirm, the rest of the code runs anyways. I tried inserting the rest of the code after the first then statement, but that didn't work either. Any advice?

#!/bin/bash

#User passes in two arguments 

read -p "Confirm: Your file name is $1 and the number of copies is $2. Press Y for yes N for no " -n 1 -r
echo    
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "Cloning files..."
fi

DIR="."

function list_files()
{
if ! test -d "$1" 
then echo "$1"; return;
fi

cd ... || $1
echo; echo "$(pwd)":; #Display Directory name

for i in *
do
if test -d "$i" #if dictionary
then 
list_files "$i" #recursively list files
cd ..
else
echo "$i"; #Display File name
fi

done }

if [ $# -eq 0 ] then list_files . exit 0 fi

for i in "$@*" do DIR=$1 list_files "$DIR" shift 1 #To read next directory/file name done

if [ ! -f "$1" ]
then echo "File $1 does not exist" exit 1 fi

for ((i=0; i if [ "$status" -eq 0 ] then echo 'File copied succeaful' else echo 'Problem copying' fi

Aucun commentaire:

Enregistrer un commentaire