Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using space in case pattern in Bash

Tags:

bash

I need find solution for using space in pattern in case. I have this function with case

  setParam() {
    case "$1" in
          0624)
            # do something - download file from url
          ;;
          del-0624)
            # do something - delete file from local machine
            exit 0
          ;;
      # Help
          *|''|h|help)
            printHelp
            exit 0
          ;;
    esac
  }

  for PARAM in $*; do
    setParam "$PARAM"
  done

Paramter "0624" is for run function for download files from url. Paramter "del-0624" is for deleting files in local machine.

Question: It is possible use paramter "del 0624"(with space)? I have problem with space in parameter in case.

like image 568
skyndas Avatar asked Apr 06 '15 11:04

skyndas


People also ask

How do you represent a space in bash?

To cd to a directory with spaces in the name, in Bash, you need to add a backslash ( \ ) before the space. In other words, you need to escape the space.

Does spacing matter in bash?

The lack of spaces is actually how the shell distinguishes an assignment from a regular command. Also, spaces are required around the operators in a [ command: [ "$timer"=0 ] is a valid test command, but it doesn't do what you expect because it doesn't recognize = as an operator.

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What does %% mean in bash?

The operator "%" will try to remove the shortest text matching the pattern, while "%%" tries to do it with the longest text matching. Follow this answer to receive notifications.


1 Answers

You need to use double quotes in case. Also script should be run as ./script "del 0624".

setParam() {
    case "$1" in
          "0624")
            # do something - download file from url
          ;;
          "del-0624")
            # do something - delete file from local machine
            exit 0
          ;;
          "del 0624")
            # do something - delete file from local machine
            exit 0
          ;;
      # Help
          *|''|h|help)
            printHelp
            exit 0
          ;;
    esac
  }
like image 193
Arjun Mathew Dan Avatar answered Oct 13 '22 00:10

Arjun Mathew Dan