0

I have a list of text files in a directory which I want to search from terminal. To not make myself think too much, I want to be able to search for few words, example search one two three if there is a file that has one of more of those words in its name, return it. This is the if statements I have:

snip-list() {
    for filename in ~/notes/*; do
        file=`basename "$filename"`
        fs=${file%.*}
        if [[ $fs =~ $1 ]] && [[ $fs =~ $2 ]] && [[ $fs =~ $3 ]] && [[ $fs =~ $4 ]] && [[ $fs =~ $5 ]]; then
            echo $fs
            # exit
        elif [[ $fs =~ $1 ]] && [[ $fs =~ $2 ]] && [[ $fs =~ $3 ]] && [[ $fs =~ $4 ]]; then
            echo $fs
            # exit
        elif [[ $fs =~ $1 ]] && [[ $fs =~ $2 ]] && [[ $fs =~ $3 ]]; then
            echo $fs
            # exit
        elif [[ $fs =~ $1 ]] && [[ $fs =~ $2 ]]; then
            echo $fs
            # exit
        elif [[ $fs =~ $1 ]]; then
            echo $fs
        fi
    done
}

I'll be adding files to this directory, so if I run that statement as is, I'll get so many returned that it be useless (at least I'd have to scroll to the top to see the first results).

If there a way to make it so that "if user searched 5 terms and they return something, don't run the other 4 conditions

I have tried running exit after each statement, that's the kind of thing I want by I need the terminal window to stay open.

  • 1) That's what elif is for ("else if"). 2) exit doesn't close the terminal window. Can you edit your question with a complete code sample, more detail of what you're trying, what your directory looks like, and how you're running this? – Michael Homer Mar 21 '16 at 4:28
  • @MichaelHomer I added the entire function. If I remove the comment from exit, the if statement quickly returns and then the window closes – relidon Mar 21 '16 at 4:36
  • 1
    are you running that function from a shell command line? BTW, use return rather than exit to return from a shell function. if using bash, type help return for details. otherwise, see your shell's man page. – cas Mar 21 '16 at 4:37
  • I guess you want return. You are getting the behaviour you want of "if the user searched 5 terms and they return something, don't run the other 4 conditions". It's when they don't all match that you're having a problem. – Michael Homer Mar 21 '16 at 4:43
  • @MichaelHomer that sounds correct and help return says the same thing but it's not working! Would the file type matter? I have the script in a .bashrc file and then added this line . ~/my_bash_scripts/my-codes.bashrc inside ~/.bashrc file. And every time I make changes, I source the main file . ~/.bashrc – relidon Mar 21 '16 at 5:54
0

Use return rather than exit to return from a shell function.

If you're using bash or some other shell with built-in help, type help return for details. Otherwise, see your shell's man page.

  • Thanks. That seems right bash help says "[return] Causes a function to exit", but is not working. This is my first time working with bash, would the file type matter? I have the script in a .bashrc file and then added this line . ~/my_bash_scripts/my-codes.bashrc inside ~/.bashrc file. And every time I make changes, I source the main file . ~/.bashrc – relidon Mar 21 '16 at 5:48
  • Nope, file type doesn't matter (aside from executable binaries and #! lines in scripts you execute rather than source, linux/unix itself doesn't really have any concept of file types. Desktop environments or GUI file browsers may distinguish between file "types" but the shell doesn't), as long as it contains only valid shell commands and can be sourced. what exactly do you mean by "is not working"? return works exactly as specified in the help and in the man page, i've used it many times. – cas Mar 21 '16 at 6:08
  • I have no idea why it didn't work, I typed a string rather than used a variable and it worked. Then went back to use of variables and it works. Thanks – relidon Mar 21 '16 at 6:11
  • BTW, you do realise that your if lines with two or more tests are ANDed? they'll only echo $fs if $fs matches ALL of the tests. i.e. what you have won't match filenames that match $2 but not $1 (but will match filenames that match $1 alone). Are you sure you don't want OR instead? if so, use if [[ $fs =~ $1|$2|$3|$4|$5 ]] ; then ... fi. An alternative would be to test $fs against $1...$5 in a loop, incrementing a counter if it matches. echo $fs and return if the counter is non-zero...you could even return the counter value in the return statement or in the echo statement. – cas Mar 21 '16 at 6:19
  • That's what I realised after fixing the return statement. I saw this comment late. I ended up using this if [[ $fs =~ .*$1*.* ]]; then for the moment. The loop sounds good but I have to search how to go that. I'm try to do this: I say I have a file name one-two-three-four-five' and one called one-two` if I type one-two I don't want the first to display. But for now I have no idea. I thought the ifs I had would do the trick but they don't. This if [[ $fs =~ .*$1*.* ]]; then works best - which what I had 10 hours ago :) – relidon Mar 21 '16 at 6:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.