Frage

I have this function:

find () {
  case "$1" in
  package)
      pacman -Ss
      ;;
  file)
      echo "Find file"
      ;;
  *)
      echo "You cannot find something like this."
     ;;
  esac
}

My goal is to be able to do simething like find package foo. However it looks like the the foo is not passed as argument to pacman. How can I fix that?

War es hilfreich?

Lösung 2

This is what you need then. Try this:

find () {
  case "$1" in
  package)
      shift
      pacman -Ss $@
      ;;
  file)
      echo "Find file"
      ;;
  *)
      echo "You cannot find something like this."
     ;;
  esac
}

Andere Tipps

Came up with the following simple solution for a variant amount of given arguments, let's say you want to slice the first two strings out.

So, say you were taking in some arguments and you know how many arguments you wish to exclude, you can slice it up like this

#!/bin/bash
array=${@: (($#-2)),-1}
for element in $array; do
    echo $element
done

An example of using this, where the -2 in (($#-2)) is the number of arguments you wish to exclude from the beginning.

$ ./script.sh some arguments given to the script
$ given
$ to
$ the
$ script

Oh, I'm using zsh btw, so - apologies if there's something in there unsupported. don't think so.

Try this:

find () {
  case "$1" in
  package)
      pacman -Ss $2
      ;;
  file)
      echo "Find file"
      ;;
  *)
      echo "You cannot find something like this."
     ;;
  esac
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top