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
Related
Made a script that the user gives a "parameter" and it prints out if it is a file, directory or non of them. This is it :
#!/bin/bash
read parametros
for filename in *
do
if [ -f "$parametros" ];
then
echo "$parametros is a file"
elif [ -d "$parametros" ];
then
echo "$parametros is a directory"
else
echo " There is not such file or directory"
fi
exit
done
Altough i want the user to be allowed to give only one word as a parameter. How do i make this happen ? (For example if user press space after first word there would be an error message showing "wrong input")
#!/bin/bash
read parametros
if [[ "$parametros" = *[[:space:]]* ]]
then
echo "wrong input"
elif [[ -f "$parametros" ]]
then
echo "$parametros is a file"
elif [[ -d "$parametros" ]]
then
echo "$parametros is a directory"
else
echo " There is not such file or directory"
fi
See http://mywiki.wooledge.org/BashFAQ/031 for the difference between [...] and [[...]].
You have to use the $#. It gives the number of the parameters.
The code will be something like:
if [ "$#" -ne 1 ]; then
printf 'ERROR!\n'
exit 1
fi
First, I'm curious why you want to restrict to one word - a file or directory could have spaces in it, but maybe you are preventing that somehow in your context.
Here are a few ways you could approach it:
Validate the input after they enter it - check if it has any spaces, eg: if [[ "parametros" == *" " ]]; then...
Get one character at a time in a while loop, eg with: read -n1 char
Show an error if it's a space
Break the loop if it's 'enter'
Build up the overall string from the entered characters
1 is obviously much simpler, but maybe 2 is worth the effort for the instant feedback that you are hoping for?
I'm trying to create a simple log file called "users.txt" that contains the username, full name, and home directory of the username entered. I also want to include the time that the script was initially running.
Any suggestions?
The script name is called catbash.sh
I have tried things such as
catbash.sh > user.txt
But I have no idea how to get specific information etc.
clear
LOGFILE=/home/student/gg193/FileTypes/TextFiles/ShellScripts/user.txt
read -p "What is your username?" username
read -p "May I know your name please? " name surname
echo "$(date) $username $name $surname $LOGFILE" >> $LOGFILE
TIME=$(date "+%H")
if [ $TIME -ge 0 -a $TIME -lt 12 ]
then
echo "\nGood Morning, $surname"
elif [ $TIME -ge 12 -a $TIME -lt 18 ]
then
echo "\nGood afternoon $surname"
else
echo "\nGood evening $surname"
fi
echo "1. nonblank\n2. number\n3. ends\n4. nends\n"
while :
do
read INPUT_STRING
case $INPUT_STRING in
nonblank)
NonEmptyLine=$(cat catbash.sh | sed '/^\s*$/d' | wc -l)
echo "\nNumber of Non-Empty Lines are:" $NonEmptyLine
break
;;
number)
EmptyLine=$(grep -cvP '\S' catbash.sh)
echo "\nNumber of Empty Lines are:" $EmptyLine
break
;;
ends)
echo "================================================================================="
sed 's/$/$/' catbash.sh
echo "================================================================================="
break
;;
nends)
NonEmptyLine=$(cat catbash.sh | sed '/^\s*$/d' | wc -l)
echo "\nNumber of Non-Empty Lines are:" $NonEmptyLine
EmptyLine=$(grep -cvP '\S' catbash.sh)
echo "\nNumber of Empty Lines are:" $EmptyLine
echo "================================================================================="
sed 's/$/$/' catbash.sh
echo "================================================================================="
break
;;
*)
echo "\nSorry, I don't understand"
;;
esac
done
DURATION=$(ps -o etime= -p "$$")
echo "\nAmount of time that has passed since the script was initially executed:" $DURATION
echo "\nThanks for using catbash.sh!"
You have to do the logging from inside the sh script. For ex, you could echo the info that you want on the terminal (which you are already doing) and then append | tee -a $LOGFILE (define LOGFILE at the top so you only have to change once if you need a different file name/location).
As further improvements you can look at defining your own logger function or even explore existing ones. For ex a simpler logger func could take 2 args - message, file name. The message itself could be composed of the user name, date/timestamp etc as you want.
With the info currently in your question, this is the best pointer I can give.
So my professor and I worked on this for about 2 hours and couldn't figure out what the problem was so I am hoping someone can see what we missed.
askDelete()
{
echo -e " Still want to delete it? (y/n)\n"
read answer
if [ "$answer" = 'y']; then
rm $1
else
echo -e "\nFile was not removed\n"
fi
}
#############################################
clear
#script starts here
echo -e "\n\tCleaner Script\n"
dir=`pwd`
while [ "$choice" -ne 3 ] || [ "$choice" != "quit" ]
do
echo -e "\nEnter 1 to delete by filename or type the word file."
echo -e "\nEnter 2 to delete by a string within a file or type the word string"
echo -e "\nEnter 3 or quit to exit this program.\n"
read choice
case "$choice" in
1|"file") echo -e"Enter the name of the file to delete: "
read file
result=$(find . -name "$file")
if [ -z $result ]; then
echo "File not found"
else
askDelete $file
fi
;;
2|"string") echo -e "Enter the sting to delete the files that contain it: "
read searchstring
result=$(find $dir -type f -perm /400)
echo $result
for file in $result;
do
echo -e "String is $searchstring \nFile is $file"
grep –q "$searchstring" "$file"
if [ $? -eq 0 ]; then
echo "****MATCH****"
askDelete $file
fi
done
;;
3|"quit") echo -e "Exiting program"
break;;
*) echo -e "\nChoice not listed";;
esac
done
and when I do selection 2 I get to the grep and get this error message with my troubleshooting messages.
Enter the sting to delete the files that contain it:
pizza
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/smith.txt
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data2.txt
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones2.txt
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/cleaner.sh
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones.txt
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data.txt
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/smith.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data2.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones2.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/cleaner.sh
grep: pizza: No such file or directory
/home/hopper/z#/CSCI/CSCI330/Assignments/assign4/cleaner.sh:
grep –q "$searchstring" "$file"
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/jones.txt
grep: pizza: No such file or directory
String is pizza
File is /home/hopper/z#/CSCI/CSCI330/Assignments/assign4/data.txt
grep: pizza: No such file or directory
Grep also works just fine outside the BASH script with the absolute paths. Tested the if statement and if I take out the -eq it does work properly because it reads that grep did successfully run just that the directory was not found. From what I can tell it is ignoring my file and instead is using the string search as the directory.
The dash part of the '-q' argument to grep is a special non-ascii character, probably an en-dash in UTF-8, I didn't look too hard. Grep doesn't interpret the en-dash as starting an option and does a search for the string '–q' in the file list. 'pizza' is in the file list.
This can happen easily if you copy code from a web page or a word doc. Anyway, delete the -q and retype it and you script should work better.
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.
So I keep messing this up and I think where I was going wrong was that the code i'm writing needs to return only the file name and number of lines from an argument.
So using wc I need to get something to accept either 0 or 1 arguments and print out something like "The file findlines.sh has 4 lines" or if they give a ./findlines.sh Desktop/testfile they'll get the "the file testfile has 5 lines"
I have a few attempts and all of them have failed. I can't seem to figure out how to approach it at all.
Should I echo "The file" and then toss the argument name in and then add another echo for "has the number of lines [lines]"?
Sample input would be from terminal something like
>findlines.sh
Output:the file findlines.sh has 18 lines
Or maybe
>findlines.sh /home/directory/user/grocerylist
Output of 'the file grocerylist has 16 lines
#! /bin/sh -
file=${1-findfiles.sh}
lines=$(wc -l < "$file") &&
printf 'The file "%s" has %d lines\n' "$file" "$lines"
This should work:
#!/bin/bash
file="findfiles.sh"
if [ $# -ge 1 ]
then
file=$1
fi
if [ -f $file ]
then
lines=`wc -l "$file" | awk '{print $1}'`
echo "The file $file has $lines lines"
else
echo "File not found"
fi
See sch's answer for a shorter example that doesn't use awk.