How to pass multiple arguments with pipe symbol into a bash-script's case statement

Zacatexas

I have a script that organizes files in my downloads directory according to their filetype.

function moveto {
    for filename in *
    do
    case "${filename##*.}" in
        $1 ) echo "!";; # echo statement for debugging
    esac
    done
}

I have a .png file in my downloads directory and nothing else.
When I call moveto "png", the exclamation mark appears.
When I call moveto "png|jpg", the exclamation mark doesn't appear.
When I simply type png|jpg into the case statement, using no variables, the exclamation mark appears.

I've tried changing things up more than a few ways; using single quotes, double quotes, no quotes, aliases, etc., nothing seems to work. Would be great if someone could help out.

rici

The | in a case statement is part of the syntax of the case statement, so it must be part of the source code. (The same is true of the ) terminating the pattern list.)

You can get the effect you want by enabling extended globs (shopt -s extglob) and then using one:

moveto "@(png|jpg)"

Extended glob patterns are documented in the bash manual; they consist of one of the characters *, +, ?, @, or ! followed by a parenthesized list of patterns separated by |. The initial character meanings are mostly familiar:

* zero or more repetitions of any of the patterns
+ one or more repetitions of any of the patterns
? nothing or exactly one of the patterns
@ exactly one of the patterns
! does not match any of the patterns

If you wanted to get fancy, you could assemble the pattern yourself:

moveto() {
  local pattern="@($(tr ' ' '|'<<<"$*"))"
  local filename
  for filename in *; do
    case "${filename##*.}" in
      $pattern) echo "!";; # echo statement for debugging
    esac
  done
}

moveto png jpg

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Pass Multiple Arguments to Bash Script

From Dev

Pass arguments with pipe in bash

From Dev

How to pass arguments to a bash script through Dockerfile

From Dev

How to pass bash script arguments to a subshell

From Dev

How to pass arguments to function in a bash script?

From Dev

How to pass arguments to inline expect in a bash script?

From Dev

How to pass in multiple arguments to execute with .sh script

From Dev

How to pass the content of a file as multiple arguments in bash

From Java

how do i compactly pass multiple symbol arguments into a function?

From Dev

sh: 2: 1: not found - pass multiple php arguments to bash script

From Dev

Is there a way to pass arguments via pipe on bash?

From Dev

Bash script - Pass different arguments

From Dev

Bash script - Pass different arguments

From Dev

How to pass multiple files to a Bash script as variables?

From Dev

How to pass arguments in double quotes inside bash script

From Java

How to pass all arguments passed to my bash script to a function of mine?

From Dev

How to pass quoted arguments from variable to bash script

From Dev

How to pass arguments to a bash script running under MinGW on Windows

From Dev

How to pass arguments in double quotes inside bash script

From Dev

How to pass arguments to bash shell script when executing that command with at?

From Dev

How to pass all arguments to a script called by /bin/bash

From Dev

how to find bash script, then execute it and pass some arguments

From Dev

How to pass multiple arguments?

From Dev

use bash script to pass arguments to a python script

From Dev

Pass bash script arguments to ansible command in script

From Dev

Bash pipe delimited string into command's arguments

From Dev

Multiple Fortran arguments in bash script

From Dev

bash scripting - how to write an effective case statement to handle multiple conditions

From Dev

Perl inside Bash: How to read from pipe and pass arguments to perl at the same time?

Related Related

  1. 1

    Pass Multiple Arguments to Bash Script

  2. 2

    Pass arguments with pipe in bash

  3. 3

    How to pass arguments to a bash script through Dockerfile

  4. 4

    How to pass bash script arguments to a subshell

  5. 5

    How to pass arguments to function in a bash script?

  6. 6

    How to pass arguments to inline expect in a bash script?

  7. 7

    How to pass in multiple arguments to execute with .sh script

  8. 8

    How to pass the content of a file as multiple arguments in bash

  9. 9

    how do i compactly pass multiple symbol arguments into a function?

  10. 10

    sh: 2: 1: not found - pass multiple php arguments to bash script

  11. 11

    Is there a way to pass arguments via pipe on bash?

  12. 12

    Bash script - Pass different arguments

  13. 13

    Bash script - Pass different arguments

  14. 14

    How to pass multiple files to a Bash script as variables?

  15. 15

    How to pass arguments in double quotes inside bash script

  16. 16

    How to pass all arguments passed to my bash script to a function of mine?

  17. 17

    How to pass quoted arguments from variable to bash script

  18. 18

    How to pass arguments to a bash script running under MinGW on Windows

  19. 19

    How to pass arguments in double quotes inside bash script

  20. 20

    How to pass arguments to bash shell script when executing that command with at?

  21. 21

    How to pass all arguments to a script called by /bin/bash

  22. 22

    how to find bash script, then execute it and pass some arguments

  23. 23

    How to pass multiple arguments?

  24. 24

    use bash script to pass arguments to a python script

  25. 25

    Pass bash script arguments to ansible command in script

  26. 26

    Bash pipe delimited string into command's arguments

  27. 27

    Multiple Fortran arguments in bash script

  28. 28

    bash scripting - how to write an effective case statement to handle multiple conditions

  29. 29

    Perl inside Bash: How to read from pipe and pass arguments to perl at the same time?

HotTag

Archive