The main problem is here:
read $input
In bash, usually, $foo
is the value of the variable foo
. Here, you don't want the value, but the name of the variable, so it should be just:
read input
Similarly, in the if
tests, $yes
and $no
should be just yes
and no
, since you just want the strings yes
and no
there.
You could use a case
statement here, which (IMHO) makes it easier to do multiple cases based on input:
case $input in
[Yy]es) # The first character can be Y or y, so both Yes and yes work
echo "Hello!"
echo "Hello!" | festival --tts
;;
[Nn]o) # no or No
echo "Are you sure?"
echo "Are you sure?" | festival --tts
;;
*) # Anything else
echo "Please answer yes or no."
echo "Please answer yes or no." | festival --tts
;;
esac
You could wrap the two echo
statements and the use of festival
in a function to avoid repeating yourself:
textAndSpeech ()
{
echo "$@"
echo "$@" | festival --tts
}
case $input in
[Yy]es) # The first character can be Y or y, so both Yes and yes work
textAndSpeech "Hello!"
;;
[Nn]o) # no or No
textAndSpeech "Are you sure?"
;;
*) # Anything else
textAndSpeech "Please answer yes or no."
;;
esac
With $input
, bash replaces this with its value, which is nothing initially, so the read
command run is:
read
And read
by default stores the input in the variable REPLY
. So you can, if you want, eliminate the input
variable altogether and use $REPLY
instead of $input
.
Also have a look at the select
statement in Bash.
If one really really really needs to have custom cp
with custom message, then recompiling the program from source is the way to go, but a bit more sensible approach would be to write out your own message via printf
preferably, instead of trying to intercept cp
's output, and handle user choices via select
dialog. Additionally, if you're going to output error messages, they should go to stderr
stream. Finally, *.*
is unnecessary and you can just use *
glob instead.
Below is the script with a couple edits. Note that you need to uncomment #cp "$item" "$HOMEWORKDIRECTORY"
line for the actual copying to occur. For testing purposes replace cp
with echo
first.
#!/bin/bash
USAGE="Usage: ./script2.sh [hw-name]"
EINVALID="Invalid homework: $1"
SOURCEDIRECTORY="$PUBLIC/homework/$1"
HOMEWORKDIRECTORY="$HOME/homework/$1"
#COPY="$SOURCEDIRECTORY/*.*"
if [ -z "$1" ]; then
echo "$USAGE" > /dev/stderr
exit 1
fi
if [ ! -d "$SOURCEDIRECTORY" ]; then
echo "$EINVALID" > /dev/stderr
exit 2
fi
if [ ! -d "$HOMEWORKDIRECTORY" ]; then
echo "making homework1"
mkdir -p $HOMEWORKDIRECTORY
for item in "$SOURCEDIRECTORY"/*; do
skip=true
if [ -f "$HOMEWORKDIRECTORY"/"${item##*/}" ]; then
printf "%s already exists in %s.\n" "$item" "$HOMEWORKDIRECTORY"
select choice in "overwrite" "skip"; do
case "$choice" in
"overwrite") echo "Overwriting."
break;;
"skip") skip=true; break;;
esac
done
fi
if [ "$skip" = "true" ]; then
continue
fi
echo cp "$item" "$HOMEWORKDIRECTORY"
done
Best Answer
The
>
redirection is done by shell, not byecho
. In fact, the shell does the redirection before the command is even started and by default shell will overwrite any file by that name if exists.You can prevent the overwriting by shell if any file exists by using the
noclobber
shell option:Example:
To unset the option:
You won't get any option like taking user input to overwrite any existing file, without doing something manual like defining a function and use it every time.