0

How can I check if a shell command ends with some text? For instance if I type

$ http://example.com/file.webm

(ends with .webm) it will be automatically replaced with

$ wget http://example.com/file.webm

Of course only if command consists of one part.

I'm using bash as my shell.

3
  • 1
    If you specify a specific shell, such as bash or zsh, then you may get an answer for how it may be implemented in that shell. Commented Jun 29, 2016 at 9:07
  • It may be bash shell. Commented Jun 29, 2016 at 9:10
  • What do you mean by ends with some text? Commented Jun 29, 2016 at 11:46

1 Answer 1

3

bash 4 provides a hook for handling a "command not found" error. In this case, http://example.com/file.webm would not be a valid command, so define the following function in your .bashrc file:

command_not_found_handle () {
  if [[ $1 = http://*.webm ]]; then
      wget "$1"
  else
      return 127
  fi
}

When you attempt to run your URL as a command, command_not_found_handle will be called with the URL as the first argument. The function checks if the command name matches a webm URL, and if it does, runs wget with the URL as an argument. (For any other unrecognized command, just return 127, a the command is still unrecognized.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.