Shell function uses only the first argument
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm writing a script to customize a clean installation of Linux and there are several sections where it runs apt-get install. I've encountered some issues with my connection where it will occasionally stop working which can cause apt-get to fail and when this happens the script would continue to process the other commands.
The following script works, except now I'm hitting another issue: if the function is issued as aptinstall firefox firefox-locale-en
for example, it only installs the first package that's listed.
I modified the script so that apt-get is run within a function that will retry if it fails up to 5 times:
# Install package - will retry download if connection fails up to 5 times and abort if unsuccessful
function aptinstall ()
local __pkg=$1
set +x
n=0
until [ $n -ge 5 ]
do
apt-get install -y $__pkg && break
n=$[$n+1]
sleep 15 # Wait 15 seconds before trying to download and install packages again
done
if [ "$n" -eq 5 ]; then
echo >&2 "An error has occurred. Please check your network connection."
exit 1
fi
Can anyone help me figure out why only the first package is installed? Thanks!
bash shell-script apt
up vote
0
down vote
favorite
I'm writing a script to customize a clean installation of Linux and there are several sections where it runs apt-get install. I've encountered some issues with my connection where it will occasionally stop working which can cause apt-get to fail and when this happens the script would continue to process the other commands.
The following script works, except now I'm hitting another issue: if the function is issued as aptinstall firefox firefox-locale-en
for example, it only installs the first package that's listed.
I modified the script so that apt-get is run within a function that will retry if it fails up to 5 times:
# Install package - will retry download if connection fails up to 5 times and abort if unsuccessful
function aptinstall ()
local __pkg=$1
set +x
n=0
until [ $n -ge 5 ]
do
apt-get install -y $__pkg && break
n=$[$n+1]
sleep 15 # Wait 15 seconds before trying to download and install packages again
done
if [ "$n" -eq 5 ]; then
echo >&2 "An error has occurred. Please check your network connection."
exit 1
fi
Can anyone help me figure out why only the first package is installed? Thanks!
bash shell-script apt
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm writing a script to customize a clean installation of Linux and there are several sections where it runs apt-get install. I've encountered some issues with my connection where it will occasionally stop working which can cause apt-get to fail and when this happens the script would continue to process the other commands.
The following script works, except now I'm hitting another issue: if the function is issued as aptinstall firefox firefox-locale-en
for example, it only installs the first package that's listed.
I modified the script so that apt-get is run within a function that will retry if it fails up to 5 times:
# Install package - will retry download if connection fails up to 5 times and abort if unsuccessful
function aptinstall ()Â
for __pkg do
â Scott
Oct 14 '17 at 21:38
1
Replacing do withfor __pkg do
results in install.sh: line 39: syntax error near unexpected token `' where the end of the function is. I must be missing something but I'm not sure what it is, my bash scripting skills have gotten pretty rusty.
â user255641
Oct 14 '17 at 22:12
for __pkg do
â Scott
Oct 14 '17 at 21:38
for __pkg do
â Scott
Oct 14 '17 at 21:38
1
1
Replacing do with
for __pkg do
results in install.sh: line 39: syntax error near unexpected token `}' where the end of the function is. I must be missing something but I'm not sure what it is, my bash scripting skills have gotten pretty rusty.â user255641
Oct 14 '17 at 22:12
Replacing do with
for __pkg do
results in install.sh: line 39: syntax error near unexpected token `}' where the end of the function is. I must be missing something but I'm not sure what it is, my bash scripting skills have gotten pretty rusty.â user255641
Oct 14 '17 at 22:12
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Instead of using $1
use "$@"
.
You don't need the local variable __pkg. In my opinion it unnecessarily complicates the script.
Also I noticed that you're using $[expression]
for arithmetic evaluation. That syntax is deprecated. I would recommend using $((expression))
instead. For example, n=$((n+1))
.
This is beyond the scope of your question, but if you are having problems with apt-get
timing out, you may want to look into solutions to avoid that, such as setting up a local mirror.
Perfect! Thanks for this, it works great.
â user255641
Oct 14 '17 at 22:24
Or more compact, but not POSIX:until (( ++n > 5 )); do __try__; done; if (( n > 5 )); then __error__; fi
â dave_thompson_085
Oct 15 '17 at 2:35
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Instead of using $1
use "$@"
.
You don't need the local variable __pkg. In my opinion it unnecessarily complicates the script.
Also I noticed that you're using $[expression]
for arithmetic evaluation. That syntax is deprecated. I would recommend using $((expression))
instead. For example, n=$((n+1))
.
This is beyond the scope of your question, but if you are having problems with apt-get
timing out, you may want to look into solutions to avoid that, such as setting up a local mirror.
Perfect! Thanks for this, it works great.
â user255641
Oct 14 '17 at 22:24
Or more compact, but not POSIX:until (( ++n > 5 )); do __try__; done; if (( n > 5 )); then __error__; fi
â dave_thompson_085
Oct 15 '17 at 2:35
add a comment |Â
up vote
1
down vote
accepted
Instead of using $1
use "$@"
.
You don't need the local variable __pkg. In my opinion it unnecessarily complicates the script.
Also I noticed that you're using $[expression]
for arithmetic evaluation. That syntax is deprecated. I would recommend using $((expression))
instead. For example, n=$((n+1))
.
This is beyond the scope of your question, but if you are having problems with apt-get
timing out, you may want to look into solutions to avoid that, such as setting up a local mirror.
Perfect! Thanks for this, it works great.
â user255641
Oct 14 '17 at 22:24
Or more compact, but not POSIX:until (( ++n > 5 )); do __try__; done; if (( n > 5 )); then __error__; fi
â dave_thompson_085
Oct 15 '17 at 2:35
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Instead of using $1
use "$@"
.
You don't need the local variable __pkg. In my opinion it unnecessarily complicates the script.
Also I noticed that you're using $[expression]
for arithmetic evaluation. That syntax is deprecated. I would recommend using $((expression))
instead. For example, n=$((n+1))
.
This is beyond the scope of your question, but if you are having problems with apt-get
timing out, you may want to look into solutions to avoid that, such as setting up a local mirror.
Instead of using $1
use "$@"
.
You don't need the local variable __pkg. In my opinion it unnecessarily complicates the script.
Also I noticed that you're using $[expression]
for arithmetic evaluation. That syntax is deprecated. I would recommend using $((expression))
instead. For example, n=$((n+1))
.
This is beyond the scope of your question, but if you are having problems with apt-get
timing out, you may want to look into solutions to avoid that, such as setting up a local mirror.
edited Oct 14 '17 at 22:25
answered Oct 14 '17 at 22:19
RobertL
4,685523
4,685523
Perfect! Thanks for this, it works great.
â user255641
Oct 14 '17 at 22:24
Or more compact, but not POSIX:until (( ++n > 5 )); do __try__; done; if (( n > 5 )); then __error__; fi
â dave_thompson_085
Oct 15 '17 at 2:35
add a comment |Â
Perfect! Thanks for this, it works great.
â user255641
Oct 14 '17 at 22:24
Or more compact, but not POSIX:until (( ++n > 5 )); do __try__; done; if (( n > 5 )); then __error__; fi
â dave_thompson_085
Oct 15 '17 at 2:35
Perfect! Thanks for this, it works great.
â user255641
Oct 14 '17 at 22:24
Perfect! Thanks for this, it works great.
â user255641
Oct 14 '17 at 22:24
Or more compact, but not POSIX:
until (( ++n > 5 )); do __try__; done; if (( n > 5 )); then __error__; fi
â dave_thompson_085
Oct 15 '17 at 2:35
Or more compact, but not POSIX:
until (( ++n > 5 )); do __try__; done; if (( n > 5 )); then __error__; fi
â dave_thompson_085
Oct 15 '17 at 2:35
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f398159%2fshell-function-uses-only-the-first-argument%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
add a comment |Â