Shell function uses only the first argument

The name of the pictureThe name of the pictureThe name of the pictureClash 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!







share' 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
















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!







share' 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














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 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
















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











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.






share|improve this answer






















  • 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











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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





























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.






share|improve this answer






















  • 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















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.






share|improve this answer






















  • 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













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.






share|improve this answer














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.







share|improve this answer














share|improve this answer



share|improve this answer








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

















  • 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


















 

draft saved


draft discarded















































 


draft saved


draft discarded














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













































































Popular posts from this blog

How to check contact read email or not when send email to Individual?

Bahrain

Postfix configuration issue with fips on centos 7; mailgun relay