What can I do to fix an error inside of case? - linux

My code is:
-cname)
changeName="{2}"
if [ -z $changeName ]; then
echo -en "Which note name do you want to change?"
read -r $changeName
echo "What would you like to change the name to?"
read -r changeNewName
if ! [ -z "${3}" ]; then
echo "The name has to have no spaces."
exit 1
fi
if [ -f /usr/share/cnote/notes/$changeNewName ]; then
echo "That note name is already used. Please choose a new one."
exit 1
fi
cp "/usr/share/cnote/notes/${changeName}" "/usr/share/cnote/notes/${changeNewName}"
;;
If I remove this part of my case statement it works again but with that part I get that error:
root#minibian:~/cnote# ./cnote -cname
./cnote: line 73: syntax error near unexpected token ;;'
./cnote: line 73: ;;'

Your first if block is not closed. and you should not put the $ symbol ahead of changeName. So the first part must be
if [ -z $changeName ]; then
echo -en "Which note name do you want to change?"
read -r changeName
echo "What would you like to change the name to?"
read -r changeNewName
fi

Related

programming in linux bash, my program does not respond

im working in linux bash, and i have a problem, im doing a exercise what say this:
"will ask for three strings by keyboard and will test
whether three valid texts have been entered or whether
these strings correspond to directory names.
If so, list what is inside each one
of them and show it. If no three have been entered
strings or any of the three is not a directory,
display a warning indicating the error." okey, in my code, i ask the strings and then, as you can see in each if, I am comparing if they correspond to a directory name or not. However, the bash gives me an error on the line. option_4.sh: line 52: syntactic error: the end of the file was not expected.
#!/bin/bash
echo "Ask for three string from keyboard";
read -p "Give me a string:" string1;
read -p "Give me another string:" string2;
read -p "Give another string:" string3;
if [ -d "$string1" ];
cd "$directorio"
if [ -d "$string1" ];
then
echo ""
echo "The cadena1 is the name of a directory.."
echo "His content ($string1):"
ls ./"$string1"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
if [ -d "$string2" ];
cd "$directorio"
if [ -d "$string2" ];
then
echo ""
echo "The cadena2 is the name of a directory."
echo "His content ($string2):"
ls ./"$string2"
else
echo "ERROR: The cadena2 does not correspond to the name of a directory."
fi
if [ -d "$string3" ];
cd "$directorio"
if [ -d "$string3" ];
then
echo ""
echo "The cadena3 is the name of a directory."
echo "His content ($string3):"
ls ./"$string3"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
The syntax of an if statement looks like this:
if [ <some test> ]
then
<commands>
else
<commands>
fi
source (edited)
You need to change the first of the two if statements per string to
if [ -d "$string1" ];
then
cd "$directorio";
fi
Or even combine the two checks, because the next check has the same condition:
if [ -d "$string1" ];
then
cd "$directorio" # moved here
echo ""
echo "The cadena1 is the name of a directory.."
echo "His content ($string1):"
ls ./"$string1"
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi
just choose a snippet to describe it, I am not sure whether you want to use absolute path or relative path. so in your script use pushd and popd will be more convenient.
and if you want to add condition check in your script, you need add then after the keyword if, and at the end you need add keyword fi
if [ -d "$string3" ];
cd "$directorio"
if [ -d "$string3" ]; then
pushd "$directorio"
echo ""
echo "The cadena3 is the name of a directory."
echo "His content ($string3):"
ls ./"$string3"
popd
else
echo "ERROR: The cadena1 does not correspond to the name of a directory."
fi

Linux Shell Scripting: Script Check

I am new to Linux bash scripting and I can't seem to find what I'm doing wrong. Here's my code. Entering number 2 and 3, after the prompt that I ask the user my code stops it doesn't continue to the IF ELSE statements. Thank you to those who will help!
#!/bin/bash
while true
do
clear
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -e "Enter Choice:"
read answer
case "$answer" in
1) ./move_empty
exit 55 ;;
2) echo "Enter a filename"
read filename
if [ -f $filename ];
then ./file_size
fi
;;
3) echo "Enter first file:"
read filename
echo "Enter second file:"
read filename2
if [ ! -f "$filename" ];
then
echo "Supplied file name" $filename "does not exist";
if [ $filename" -nt $filename" ]; then
echo $filename "is newer"
exit 1fi
fi ;;
5) exit ;;
esac
done
If you have completed the check at ShellCheck.net, then you should have received:
$ shellcheck myscript
No issues detected!
If you didn't work it down to that point, you are not done. You have multiple quoting problems in your script and you compare $filename -nt $filename (which is always false). Small "attention to detail" issues that make a big difference. ShellCheck.net does a thorough job, but will not find logic issues, those are left to you. The cleanup of your quoting would look similar to:
#!/bin/bash
while true
do
clear
echo "Please enter one of the following options"
echo "1. Move empty files"
echo "2. Check file size"
echo "3. Which file is newer"
echo "4. File check rwx"
echo "5. Exit".
echo -n "Enter Choice: "
read -r answer
case "$answer" in
1) ./move_empty
exit 55
;;
2) echo -n "Enter a filename: "
read -r filename
if [ -f "$filename" ]
then
./file_size
fi
;;
3) echo -n "Enter first file: "
read -r filename
echo -n "Enter second file: "
read -r filename2
if [ ! -f "$filename2" ]
then
echo "Supplied file name $filename does not exist";
if [ "$filename" -nt "$filename2" ]; then
echo "$filename is newer"
exit 1
fi
fi
;;
5) exit
;;
esac
done
(note: you do not need echo -e as there are no backslash escaped characters to handle in your prompt, likely you intended -n to prevent the addition of a newline at the end of the prompt)
(also note: the use of clear, while fine for some terminals, will cause problems with others. Just be aware of the potential issue.)
If your then is on the same line with your conditional expression, e.g. if [ "$filename" -nt "$filename2" ]; then then a ';' is needed after the closing ']' to indicate a newline, otherwise, there is no need for a ';'.
Logic Problems
As discussed, the logic problems are not caught by ShellCheck and you must work though the code. It looks like you intended something like the following:
3) echo -n "Enter first file: "
read -r filename
echo -n "Enter second file: "
read -r filename2
if [ ! -f "$filename" ] || [ ! -f "$filename2" ]
then
echo "Supplied file '$filename' or '$filename2' does not exist";
exit 1
fi
if [ "$filename" -nt "$filename2" ]; then
echo "$filename is newer"
else
echo "$filename2 is newer"
fi
;;
You just have to take it line by line...
Look things over and let me know if you have further questions.

IF statement for LINUX

Modify your program so that if no matching name is found, a message is displayed: "Name 'xyz' not in directory". You could use an 'if' statement to check the value of $? to see if the grep command was successful (remember that '0' indicates success). If the grep is NOT successful, then echo the message (which includes the value of $name).
The original code was:
#!/bin/bash
name=$1
if [ "$name" = "" ]
then echo -n "Enter a name to search for: "
read name
fi
grep -I $name ~uli101/2017a/phonebook
I basically have to insert and IF statement into the program using the instructions above. I've tried many different things such as:
if [ $? = 1 ]
then echo -n "Name 'xyz' not in directory"
fi
but it is not accepting the answer. Pls help me out with this. Any help is appreciated.
This is working for me, try:
#!/bin/bash
name=$1
if [ "$name" = "" ]; then
echo -n "Enter a name to search for: "
read name
fi
grep -I $name phonebook
if [ $? = 1 ]; then
echo "Name '${name}' not in directory"
fi
Or you can change the last IF into:
grep -I $name phonebook && echo -n "Name not in directory"
Try this, it works for me.
#!/bin/bash
#
NAME=$1
if [ -z "${NAME}" ]; then
echo -n "Enter a name to search for: "
read NAME
fi
grep -I $NAME~uli101/2017a/phonebook

Linux script errors- syntax error near unexpected token fi

I have been working on this script for hours trying to find out why it doesn't run,
it keeps spitting out :
"program.sh: line 23: syntax error near unexpected tokenfi'
program.sh: line 23:fi
here is a copy of the script :
#!/bin/bash
#this is the program men
if [ $CHOICE = "1" ]; then
echo "removing old backup folder"
rm -rf ./AllBackUp
fi
echo "Backing up all files to ./Allbackup"
cp $PWD/* $PWD/AllBackUp
elif [ $CHOICE = "2" ]; then
if [ -d SelectBackup ];
rm -rf ./SelectBackup
fi
for f in $PWD; do
cp $PWD/$PATTERN $PWD/SelectBackup
done
help, I'm in a hole here!
Your second if statement is missing a then, so the fi is, indeed, unexpected. (Bash is still expecting a then.)

Delete file using linux

This code is that I want to give two directory and this code will look if these two directory contains two files that contains the same information, and asks me which file I want to delete .
I have written this code but I don't know how to write the code that will delete the file , please help
#!bin/bash
echo "give the first directory"
read firstdir
echo "give the second directory"
read seconddir
for i in ` ls $firstdir`
do
echo $i
t= `md5sum $firstdir/$i`
s= `md5sum $seconddir/$i`
if [ "$t" ! = "$s" ]
then
echo " of which directory will be eliminated? $i"
read direct
( here I want the code to delete the directory ex : delete direct/i )
fi
done
Replace:
echo " of which directory will be eliminated? $i"
read direct
( here I want the code to delete the directory ex : delete direct/i )
With:
echo "of which directory will be eliminated? $i:
1)$firstdir
2)$seconddir
"
read -p "(1/2)" direct
case $direct in
1)
rm -v $firstdir/$i
;;
2)
rm -v $seconddir/$i
;;
*)
echo "ERROR: bad value, 1 or 2 expected!" ; exit 1
esac
Ok, try this. I just made my own solution based on your requirements. I hope you like it. Thanks
#!/bin/bash
# check for a valid first directory
while true
do
echo "Please, enter the first directory"
read FIRST_DIR
if [ -d $FIRST_DIR ]; then
break
else
echo "Invalid directory"
fi
done
# check for a valid second directory
while true
do
echo "Please, give the second directory"
read SECOND_DIR
if [ -d $SECOND_DIR ]; then
break
else
echo "Invalid directory"
fi
done
for FILE in `ls $FIRST_DIR`
do
# make sure that only files will be avaluated
if [ -f $FILE ]; then
echo $SECOND_DIR/$FILE
# check if the file exist in the second directory
if [ -f $SECOND_DIR/$FILE ]; then
# compare the two files
output=`diff -c $FIRST_DIR/$FILE $SECOND_DIR/$FILE`
if [ ! $output ]; then
echo "Which file do you want to delete?
1)$FIRST_DIR/$FILE
2)$SECOND_DIR/$FILE
"
# read a choice
read -p "(1/2)" choice
# delete the chosen file
case $choice in
1)
rm -v $FIRST_DIR/$FILE
;;
2)
rm -v $SECOND_DIR/$FILE
;;
*)
echo "ERROR invalid choice!"
esac
fi
else
echo "There are no equal files in the two directories."
exit 1
fi
else
echo "There are no files to be evaluated."
fi
done

Resources