Issue
I have specified variables in the shell script as follows
USERNAME="$1"
PASSWORD="$2"
DATA="${@:3}"
DATAVERSION="$4"
when I run the script is goes something like this:
./abc.sh "name" "password" csv V1
But when here csv and V1 are considered as 3rd argument instead of considering V1 as the fourth argument Because of ${@:3} which I needed.
So how one can end this ${@:3} while passing the argument to script. So that arguments can be read?
Solution
bash command line:
One of most powerfull feature of bash is: You could try inline near every part of your script.
For making tries around arguments parsing: simply open any terminal window and try :
set -- "Full Name" "password" csv V1
From there, you're in (near) same condition than a script that was run with this arguments: myscript "Full Name" "password" csv V1
. So you could:
echo $1
Full Name
echo ${@:3}
csv V1
echo ${@:0:3}
/bin/bash Full Name password
echo ${@:1:3}
Full Name password csv
so
userName=$1
passWord=$2
datas=${*:3}
dataVersion=$4
printf '%-12s <%s>\n' userName "$userName" passWord "$passWord" \
datas "$datas" dataVersion "$dataVersion"
Must produce:
userName <Full Name>
passWord <password>
datas <csv V1>
dataVersion <V1>
Using shift
Or ...
set -- "Full Name" "password" csv V1
Then
userName=$1
shift
passWord=$1
shift
datas=$*
shift
dataVersion=$1
printf '%-12s <%s>\n' userName "$userName" passWord "$passWord" \
datas "$datas" dataVersion "$dataVersion"
will produce:
userName <Full Name>
passWord <password>
datas <csv V1>
dataVersion <V1>
Regarding tripleee's comment about capitalized variable names, my preference is to use lowerCamelCase.
Answered By - F. Hauri - Give Up GitHub Answer Checked By - Mary Flores (WPSolving Volunteer)