Reputation: 1
I have a script that takes in several arguments.
I need everything but $1
and $2
in a string.
I have tried this:
message="$*"
words= $(grep -v "$2"|"$3" $message)
but it doesn't work, it gives me the error:
./backup: line 26: First: command not found
Upvotes: 0
Views: 635
Reputation: 5139
Use shift 2
to shift the arguments along (it drops the first n arguments).
If you need "$1"
and "$2"
for later, save them in variables first.
Note that in shell, assignments to variables cannot have whitespace either side of the =
.
First=$1
Second=$2
shift 2
Message=$@
Upvotes: 3
Reputation: 52112
You can use a subarray:
$ set -- arg1 arg2 arg3 arg4
$ str=${*:3}
$ echo "$str"
arg3 arg4
More often than not, it's good practice to preserve the arguments as separate elements, though, which you can do by using $@
and assigning to a new array:
$ arr=("${@:3}")
$ declare -p arr
declare -a arr=([0]="arg3" [1]="arg4")
Notice that in str=${*:3}
, quoting isn't necessary, but in arr=("${@:3}")
, it is (or the arguments would be split on whitespace).
As for your error message: your command
words= $(grep -v "$2"|"$3" $message)
does the following:
words
to the empty string for the environment of the command (because there is a blank after =
).grep -v "$2"
and "$3" $message
. The first of these commands would just hang and wait for input; the second one tries to run the contents of $3
as a command; presumably, based on your error message, $3
contains First
.=
).Upvotes: 0
Reputation: 398
Maybe something like this?
[root@tsekmanrhel771 ~]# cat ./skip1st2.sh
#!/bin/bash
COUNT=0
for ARG in "$@"
do
COUNT=$[COUNT + 1]
if [ ${COUNT} -gt 2 ]; then
RESULT="${RESULT} ${ARG}"
fi
done
echo ${RESULT}
[root@tsekmanrhel771 ~]# ./skip1st2.sh first second third 4 5 6 7
third 4 5 6 7
Upvotes: 0