Linux Shell Scripting: Script Check - linux

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.

Related

accepting an argument and conditions Bash

I´m trying to make a script which takes a single command-line argument. Then it looks on the argument and if its a directory name, it just prints that this directory exists. If its a file name, it prints out the file exists. Otherwise, it tries to create a directory with this name and tests whether it was successful and reports this on the standard output.
my code is:
while read argument; do
if [ $argument -d ]; then
echo "Directory exists"
elif [ $argument -e ]
echo "File exists"
else
mkdir $argument
if [ $argument -d]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
done
Then I run the code ./file_name.sh argument. If I run the code like this, I get an error on line 8, which is just "else". While is probably not necessary here, it was the first option how to accept an argument from the command line that came to my mind.
As you mentioned, you need single command line argument, So no need to loop
#!/bin/bash
if [[ -z "$1" ]]; then
echo "Help : You have to pass one argument"
exit 0
fi
if [[ -d "$1" ]]; then
echo "Directory exists"
elif [[ -f "$1" ]]; then
echo "File exists"
else
mkdir "$1"
if [ -d "$1" ]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
if [ -d $1 ]; then
echo "Directory exists"
elif [ -e $1 ]; then
echo "File exists"
else
mkdir $1
if [ -d $1 ]; then
echo "Directory was created"
else
echo "Error while creating the directory"
fi
fi
I made up this solution thanks to the links provided, thank you.

Bash: Counting instances of a string in text file with a loop

I am trying to write a simple bash script in which it takes in a text file, loops through the file and tells me how many times a certain string appears in the file. I want to eventually use this for a custom log searcher (for instance, search for the words 'log in' in a particular log file, etc.), but am having some difficulty as I am relatively new to bash. I want to be able to quickly search different logs for different terms at my will and see how many times they occur. Everything works perfectly until I get down to my loops. I think that I am using grep wrong, but am unsure if that is the issue. My loop codes may seem a little strange because I have been at it for a while and have been constantly tweaking things. I have done a bunch of searching but I feel like I am the only one who has ever had this issue (hopefully not because it is incredibly simple and I just suck). Any and all help is greatly appreciated, thanks in advance everyone.
edit: I would like to account for every instance of the string and not just
one instance per line
#!/bin/bash
echo "This bash script counts the instances of a user-defined string in a file."
echo "Enter a file to search:"
read fileName
echo " "
echo $path
if [ -f "$fileName" ] || [ -d "$fileName" ]; then
echo "File Checker Complete: '$fileName' is a file."
echo " "
echo "Enter a string that you would like to count the occurances of in '$fileName'."
read stringChoice
echo " "
echo "You are looking for '$stringChoice'. Counting...."
#TRYING WITH A WHILE LOOP
count=0
cat $fileName | while read line
do
if echo $line | grep $stringChoice; then
count=$[ count + 1 ]
done
echo "Finished processing file"
#TRYING WITH A FOR LOOP
# count=0
# for i in $(cat $fileName); do
# echo $i
# if grep "$stringChoice"; then
# count=$[ $count + 1 ]
# echo $count
# fi
# done
if [ $count == 1 ] ; then
echo " "
echo "The string '$stringChoice' occurs $count time in '$fileName'."
elif [ $count > 1 ]; then
echo " "
echo "The string '$stringChoice' occurs $count times in '$fileName'."
fi
elif [ ! -f "$fileName" ]; then
echo "File does not exist, please enter the correct file name."
fi
To find and count all occurrences of a string, you could use grep -o which matches only the word instead of the entire line and pipe the result to wc
read string; grep -o "$string" yourfile.txt | wc -l
You made basic syntax error in the code. Also, the variable of count was never updating as the the while loop was being executed in a subshell and thus the updated count value was never reflecting back.
Please change your code to the following one to get desired result.
#!/bin/bash
echo "This bash script counts the instances of a user-defined string in a file."
echo "Enter a file to search:"
read fileName
echo " "
echo $path
if [ -f "$fileName" ] ; then
echo "File Checker Complete: '$fileName' is a file."
echo " "
echo "Enter a string that you would like to count the occurances of in '$fileName'."
read stringChoice
echo " "
echo "You are looking for '$stringChoice'. Counting...."
#TRYING WITH A WHILE LOOP
count=0
while read line
do
if echo $line | grep $stringChoice; then
count=`expr $count + 1`
fi
done < "$fileName"
echo "Finished processing file"
echo "The string '$stringChoice' occurs $count time in '$fileName'."
elif [ ! -f "$fileName" ]; then
echo "File does not exist, please enter the correct file name."
fi

syntax error near unexpected token `echo'

I am a newbie to stack overflow so please bear with the formatting
It is a genuine problem
I am creating a program to copy ,remove and move a file but still cannot resolve the echo error that bugs it, what should i do?
I have read the other related question which tells to include a ; but this too has not resolved the situation
#!/bin/bash
echo "Menu "
echo "1. Copy a File "
echo "2. Remove a file "
echo "3. Move a file"
echo "4. Quit"
echo "Enter ur Choice \c"
read Choice
case " $Choice " in
1. echo "Enter File name to copy \c"
read f1
echo " Enter File name \c "
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo "$f1 does not exist"
fi
;;
2. echo "Enter the File to be removed "
read r1
if [ -f $r1 ]
then
rm -i $r1
else
echo "$r1 file does not exist "
fi
;;
3.
echo "Enter File name to move \c"
read f1
echo "Enter destination \c "
read f2
if [ -f $f1 ]
then
if [ -d $f2 ]
then
mv $f1 $f2
fi
else
echo "$f1 does not exist"
fi
;;
4.
echo "Exit......."
exit;;
esac
ERRORS
./script13.sh: line 10: syntax error near unexpected token `echo'
./script13.sh: line 10: ` 1. echo "Enter File name to copy \c " '
root#Kalilinux1:~/bin#
You lack the parenthesis, so :
#!/bin/bash
echo "Menu "
echo "1. Copy a File "
echo "2. Remove a file "
echo "3. Move a file"
echo "4. Quit"
read -p "Enter ur Choice >>> "
case " $Choice " in
1) echo "Enter File name to copy \c"
read f1
echo " Enter File name \c "
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo "$f1 does not exist"
fi
;;
2) echo "Enter the File to be removed "
read r1
if [ -f $r1 ]
then
rm -i $r1
else
echo "$r1 file does not exist "
fi
;;
3)
echo "Enter File name to move \c"
read f1
echo "Enter destination \c "
read f2
if [ -f $f1 ]
then
if [ -d $f2 ]
then
mv $f1 $f2
fi
else
echo "$f1 does not exist"
fi
;;
4)
echo "Exit......."
exit;;
esac
Instead of 1. in line 10, you should use 1). The correct way to do a case statement is:
case "$Choice" in
1) ...
;;
2) ...
;;
Also, remove the spaces in " $Choice ".

want to make multiple flags in script?

I want to make a multiple flags for my script. And didn't work like what I want it.
#!/bin/bash
function wherecpy
{
echo "Plaes write where the files are located: "
read placefile
cd $placefile
}
function funcpy
{
wherecpy
echo ""
echo "Please choose the following:"
echo ""
echo "1. Interactive copy, answer yes or no (y/n) before doing the copy"
echo "2. Make backups of existing destination files"
echo "3. Preserve file attributes"
echo "4. Do a recursively copy"
echo "5. Back to main menu"
echo ""
echo -n "Enter Your Selection: "
read selection
echo ""
case $selection in
1) a="-i" ;;
2) b="--backup" ;;
3) c="--preserve=all" ;;
4) d="-r" ;;
0) return 0 ;;
esac
echo "Type the name of the file you wish to copy/backup: "
read file
echo "Type the name of the destination file/directory: "
read dest
cp $a $b $c $d "$file" "$dest"
if [ $? == 0 ]; then
echo "Success"
else
echo "failed."
fi
}
I want it to work like that :
Please type in the source file(s) to copy:
File1 file2 file3
Please type in the destination:
dir1
Copy successful.
Press any key to return to main menu.
Use getopts :
There are many online resources you can take reference from

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