-1

i have to do a script that allows user to search for a file. I am total newbie in bash so you have to forgive me. I have problems with 3. Maximum file size, 4. Last modification date and 6. File content.

In option 3 Max file size, when i type anything program excludes any file,

In option 4 it gives me an error "Invalid argument for -mtime and in

In option 6 it is "unknown expression -l for -grep

Here is some of my code:

    #!/bin/bash

OPTION=0


MAX_SIZE=""
LAST_MOD_DATA

CONTENT=""

SEARCH_CONTENT=""


while [ $OPTION -ne 8 ]
do
    read OPTION
    
    if [ $OPTION -eq 3 ]
    then
        echo "Enter max allowed file size:"
        read MAX_SIZE
        SEARCH_CONTENT = $SEARCH_CONTENT' -size '$MAX_SIZE
    fi
    
    if [ $OPTION -eq 4 ]
    then
        echo "Enter the last day the file was modified:"
        read LAST_MOD_DAY
        SEARCH_CONTENT = $SEARCH_CONTENT' -mtime '$LAST_MOD_DAY
    fi
    
     if [ $OPTION -eq 6 ]
    then
        echo "Enter the file content:"
        read CONTENT
        SEARCH_CONTENT = $SEARCH_CONTENT' -exec grep -l '$CONTENT $FILE_NAME
    fi
    
    if [ $OPTION -eq 7 ]
    then
        echo "Searched files:"
        echo find $SEARCH_CONTENT
    fi
done

echo "The end"
D W
  • 3
  • 2
  • 2
    Start by checking your script on [https://www.shellcheck.net/](https://www.shellcheck.net/) – 0stone0 Nov 24 '20 at 16:38
  • 1
    Avoid the spaces around `=`. https://stackoverflow.com/questions/2268104/command-not-found-error-in-bash-variable-assignment/2268117#2268117 – William Pursell Nov 24 '20 at 16:49
  • 1
    Don't put code in variables. Use a function. c.f. https://mywiki.wooledge.org/BashFAQ/050 – Paul Hodges Nov 24 '20 at 16:51
  • 1
    And use a case statement: `while read; do case $REPLY in 8) break;; 3) ...;; *) : default ;; esac; done` – William Pursell Nov 24 '20 at 16:52
  • 1
    Each question should be about only one problem, with the shortest script that demonstrates that specific problem when run without changes. Don't ask multiple questions in one post. – Charles Duffy Nov 24 '20 at 16:54

1 Answers1

0

You are setting $SEARCH_CONTENT incorrectly. First of all, there shouldn't be spaces around the equal sign. Also, you will want to surround the new value with double quotes. Finally, you should surround the new values with the single quotes.

SEARCH_CONTENT="$SEARCH_CONTENT -size '$MAX_SIZE'"

The double quotes make it a single value, allowing for the variable interpolations. The single quotes ensure that find will treat the new value as a single argument, mainly if it includes spaces.

You also should review the -exec argument with find. You'll need to add a ';' to the end of the command. Also, this script will more than likely fail if the user selects an option more than once.

UncleCarl
  • 331
  • 2
  • 15
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section *Answer Well-Asked Questions*, and therein the bullet point regarding questions that "have already been asked and answered many times before". – Charles Duffy Nov 24 '20 at 16:55
  • Also, adding single-quotes to the variable's value does *not* work. See ["Why does shell ignore quoting characters in arguments passed to it through variables?"](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia) and [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050) – Gordon Davisson Nov 24 '20 at 18:40