Using arrays in shell script
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm trying to store the pathway to a website in an array, but am having trouble.
It is working in the for loop - when I echo the contents of the array, it prints correctly.
In the If/Else, though, I am trying to store a specific part of the array in another variable, and that is not working. When I echo the specific part of the array, it prints nothing.
Here is the code:
#! /bin/sh
SITES=()
i=0
for d in /var/www/sites/*/;
do (
PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY"
); let "i++"
done
read -p 'Enter the number of the website you want to restore. Enter "exit" to quit. ' url
if [ "$url" = "exit" ]
then
exit 0
else
RESTORE_URL=$SITES[url]
fi
shell-script array
 |Â
show 6 more comments
up vote
0
down vote
favorite
I'm trying to store the pathway to a website in an array, but am having trouble.
It is working in the for loop - when I echo the contents of the array, it prints correctly.
In the If/Else, though, I am trying to store a specific part of the array in another variable, and that is not working. When I echo the specific part of the array, it prints nothing.
Here is the code:
#! /bin/sh
SITES=()
i=0
for d in /var/www/sites/*/;
do (
PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY"
); let "i++"
done
read -p 'Enter the number of the website you want to restore. Enter "exit" to quit. ' url
if [ "$url" = "exit" ]
then
exit 0
else
RESTORE_URL=$SITES[url]
fi
shell-script array
why not useselect path in /var/www/sites/*; do [...]; done
?
â DopeGhoti
Oct 20 '17 at 22:19
You are missing a$
onurl
. Should be:RESTORE_URL=$SITES[$url]
. Also I guess it's not functionally any different but you should usedeclare -a SITES
instead ofSITES=()
â Jesse_b
Oct 20 '17 at 22:20
Does /bin/sh support arrays?
â Jeff Schaller
Oct 20 '17 at 22:37
@Jesse_b I had tried it with both of those, previously, and it still did not work.
â Kimberlie Davis
Oct 20 '17 at 22:52
@JeffSchaller, I guess it depends on what OS is being used./bin/sh
is normally a symlink to bash though. @Kimberlie, try again. Your issue is thaturl
doesn't have a$
.
â Jesse_b
Oct 20 '17 at 23:35
 |Â
show 6 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to store the pathway to a website in an array, but am having trouble.
It is working in the for loop - when I echo the contents of the array, it prints correctly.
In the If/Else, though, I am trying to store a specific part of the array in another variable, and that is not working. When I echo the specific part of the array, it prints nothing.
Here is the code:
#! /bin/sh
SITES=()
i=0
for d in /var/www/sites/*/;
do (
PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY"
); let "i++"
done
read -p 'Enter the number of the website you want to restore. Enter "exit" to quit. ' url
if [ "$url" = "exit" ]
then
exit 0
else
RESTORE_URL=$SITES[url]
fi
shell-script array
I'm trying to store the pathway to a website in an array, but am having trouble.
It is working in the for loop - when I echo the contents of the array, it prints correctly.
In the If/Else, though, I am trying to store a specific part of the array in another variable, and that is not working. When I echo the specific part of the array, it prints nothing.
Here is the code:
#! /bin/sh
SITES=()
i=0
for d in /var/www/sites/*/;
do (
PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY"
); let "i++"
done
read -p 'Enter the number of the website you want to restore. Enter "exit" to quit. ' url
if [ "$url" = "exit" ]
then
exit 0
else
RESTORE_URL=$SITES[url]
fi
shell-script array
edited Oct 20 '17 at 22:09
amphibient
4,42993469
4,42993469
asked Oct 20 '17 at 22:04
Kimberlie Davis
82
82
why not useselect path in /var/www/sites/*; do [...]; done
?
â DopeGhoti
Oct 20 '17 at 22:19
You are missing a$
onurl
. Should be:RESTORE_URL=$SITES[$url]
. Also I guess it's not functionally any different but you should usedeclare -a SITES
instead ofSITES=()
â Jesse_b
Oct 20 '17 at 22:20
Does /bin/sh support arrays?
â Jeff Schaller
Oct 20 '17 at 22:37
@Jesse_b I had tried it with both of those, previously, and it still did not work.
â Kimberlie Davis
Oct 20 '17 at 22:52
@JeffSchaller, I guess it depends on what OS is being used./bin/sh
is normally a symlink to bash though. @Kimberlie, try again. Your issue is thaturl
doesn't have a$
.
â Jesse_b
Oct 20 '17 at 23:35
 |Â
show 6 more comments
why not useselect path in /var/www/sites/*; do [...]; done
?
â DopeGhoti
Oct 20 '17 at 22:19
You are missing a$
onurl
. Should be:RESTORE_URL=$SITES[$url]
. Also I guess it's not functionally any different but you should usedeclare -a SITES
instead ofSITES=()
â Jesse_b
Oct 20 '17 at 22:20
Does /bin/sh support arrays?
â Jeff Schaller
Oct 20 '17 at 22:37
@Jesse_b I had tried it with both of those, previously, and it still did not work.
â Kimberlie Davis
Oct 20 '17 at 22:52
@JeffSchaller, I guess it depends on what OS is being used./bin/sh
is normally a symlink to bash though. @Kimberlie, try again. Your issue is thaturl
doesn't have a$
.
â Jesse_b
Oct 20 '17 at 23:35
why not use
select path in /var/www/sites/*; do [...]; done
?â DopeGhoti
Oct 20 '17 at 22:19
why not use
select path in /var/www/sites/*; do [...]; done
?â DopeGhoti
Oct 20 '17 at 22:19
You are missing a
$
on url
. Should be: RESTORE_URL=$SITES[$url]
. Also I guess it's not functionally any different but you should use declare -a SITES
instead of SITES=()
â Jesse_b
Oct 20 '17 at 22:20
You are missing a
$
on url
. Should be: RESTORE_URL=$SITES[$url]
. Also I guess it's not functionally any different but you should use declare -a SITES
instead of SITES=()
â Jesse_b
Oct 20 '17 at 22:20
Does /bin/sh support arrays?
â Jeff Schaller
Oct 20 '17 at 22:37
Does /bin/sh support arrays?
â Jeff Schaller
Oct 20 '17 at 22:37
@Jesse_b I had tried it with both of those, previously, and it still did not work.
â Kimberlie Davis
Oct 20 '17 at 22:52
@Jesse_b I had tried it with both of those, previously, and it still did not work.
â Kimberlie Davis
Oct 20 '17 at 22:52
@JeffSchaller, I guess it depends on what OS is being used.
/bin/sh
is normally a symlink to bash though. @Kimberlie, try again. Your issue is that url
doesn't have a $
.â Jesse_b
Oct 20 '17 at 23:35
@JeffSchaller, I guess it depends on what OS is being used.
/bin/sh
is normally a symlink to bash though. @Kimberlie, try again. Your issue is that url
doesn't have a $
.â Jesse_b
Oct 20 '17 at 23:35
 |Â
show 6 more comments
2 Answers
2
active
oldest
votes
up vote
2
down vote
This sounds like a good place to use a select
statement:
select path in /var/www/sites/* EXIT; do
if [[ "EXIT" = "$path" ]]; then
exit 0
else
: do stuff here referencing $path
fi
done
I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server.
â Kimberlie Davis
Oct 20 '17 at 22:55
1
@KimberlieDavis: that's almost exactly whatselect
does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array.
â dave_thompson_085
Oct 21 '17 at 8:16
Okay, I get it. That worked - thanks!!
â Kimberlie Davis
Oct 24 '17 at 21:30
add a comment |Â
up vote
0
down vote
Your loop (with my indentation):
for d in /var/www/sites/*/; do
( PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY" )
let "i++"
done
Notice how most of the loop body is in a subshell? This means that the changes to those variables won't be seen outside of that subshell, and that they are reset for each iteration in the loop.
Ether change the (...)
to ...;
or remove them completely.
A better alternative for the script as a whole would be to use select
as DopeGhoti explains.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
This sounds like a good place to use a select
statement:
select path in /var/www/sites/* EXIT; do
if [[ "EXIT" = "$path" ]]; then
exit 0
else
: do stuff here referencing $path
fi
done
I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server.
â Kimberlie Davis
Oct 20 '17 at 22:55
1
@KimberlieDavis: that's almost exactly whatselect
does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array.
â dave_thompson_085
Oct 21 '17 at 8:16
Okay, I get it. That worked - thanks!!
â Kimberlie Davis
Oct 24 '17 at 21:30
add a comment |Â
up vote
2
down vote
This sounds like a good place to use a select
statement:
select path in /var/www/sites/* EXIT; do
if [[ "EXIT" = "$path" ]]; then
exit 0
else
: do stuff here referencing $path
fi
done
I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server.
â Kimberlie Davis
Oct 20 '17 at 22:55
1
@KimberlieDavis: that's almost exactly whatselect
does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array.
â dave_thompson_085
Oct 21 '17 at 8:16
Okay, I get it. That worked - thanks!!
â Kimberlie Davis
Oct 24 '17 at 21:30
add a comment |Â
up vote
2
down vote
up vote
2
down vote
This sounds like a good place to use a select
statement:
select path in /var/www/sites/* EXIT; do
if [[ "EXIT" = "$path" ]]; then
exit 0
else
: do stuff here referencing $path
fi
done
This sounds like a good place to use a select
statement:
select path in /var/www/sites/* EXIT; do
if [[ "EXIT" = "$path" ]]; then
exit 0
else
: do stuff here referencing $path
fi
done
answered Oct 20 '17 at 22:22
DopeGhoti
40.7k54979
40.7k54979
I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server.
â Kimberlie Davis
Oct 20 '17 at 22:55
1
@KimberlieDavis: that's almost exactly whatselect
does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array.
â dave_thompson_085
Oct 21 '17 at 8:16
Okay, I get it. That worked - thanks!!
â Kimberlie Davis
Oct 24 '17 at 21:30
add a comment |Â
I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server.
â Kimberlie Davis
Oct 20 '17 at 22:55
1
@KimberlieDavis: that's almost exactly whatselect
does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array.
â dave_thompson_085
Oct 21 '17 at 8:16
Okay, I get it. That worked - thanks!!
â Kimberlie Davis
Oct 24 '17 at 21:30
I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server.
â Kimberlie Davis
Oct 20 '17 at 22:55
I'm not sure select is best for what I need...what the script is doing is listing the sites from the server, and then connecting to another server to pull a backup file. So, the path is just for the sake of displaying what sites are on the server (as they should also be on the backup server), and then I take the filename from there to pull from the backup server. What I was hoping to use an array for is to connect each filename to a number, that the user can enter when going to pull from the backup server.
â Kimberlie Davis
Oct 20 '17 at 22:55
1
1
@KimberlieDavis: that's almost exactly what
select
does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array.â dave_thompson_085
Oct 21 '17 at 8:16
@KimberlieDavis: that's almost exactly what
select
does; it displays a numbered list of strings and lets the user enter a number to select one of the strings. Without an array.â dave_thompson_085
Oct 21 '17 at 8:16
Okay, I get it. That worked - thanks!!
â Kimberlie Davis
Oct 24 '17 at 21:30
Okay, I get it. That worked - thanks!!
â Kimberlie Davis
Oct 24 '17 at 21:30
add a comment |Â
up vote
0
down vote
Your loop (with my indentation):
for d in /var/www/sites/*/; do
( PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY" )
let "i++"
done
Notice how most of the loop body is in a subshell? This means that the changes to those variables won't be seen outside of that subshell, and that they are reset for each iteration in the loop.
Ether change the (...)
to ...;
or remove them completely.
A better alternative for the script as a whole would be to use select
as DopeGhoti explains.
add a comment |Â
up vote
0
down vote
Your loop (with my indentation):
for d in /var/www/sites/*/; do
( PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY" )
let "i++"
done
Notice how most of the loop body is in a subshell? This means that the changes to those variables won't be seen outside of that subshell, and that they are reset for each iteration in the loop.
Ether change the (...)
to ...;
or remove them completely.
A better alternative for the script as a whole would be to use select
as DopeGhoti explains.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Your loop (with my indentation):
for d in /var/www/sites/*/; do
( PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY" )
let "i++"
done
Notice how most of the loop body is in a subshell? This means that the changes to those variables won't be seen outside of that subshell, and that they are reset for each iteration in the loop.
Ether change the (...)
to ...;
or remove them completely.
A better alternative for the script as a whole would be to use select
as DopeGhoti explains.
Your loop (with my indentation):
for d in /var/www/sites/*/; do
( PATHWAY=$d
SITES+=($PATHWAY)
echo "$i: $PATHWAY" )
let "i++"
done
Notice how most of the loop body is in a subshell? This means that the changes to those variables won't be seen outside of that subshell, and that they are reset for each iteration in the loop.
Ether change the (...)
to ...;
or remove them completely.
A better alternative for the script as a whole would be to use select
as DopeGhoti explains.
answered Feb 10 at 10:42
Kusalananda
105k14209326
105k14209326
add a comment |Â
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%2f399448%2fusing-arrays-in-shell-script%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
why not use
select path in /var/www/sites/*; do [...]; done
?â DopeGhoti
Oct 20 '17 at 22:19
You are missing a
$
onurl
. Should be:RESTORE_URL=$SITES[$url]
. Also I guess it's not functionally any different but you should usedeclare -a SITES
instead ofSITES=()
â Jesse_b
Oct 20 '17 at 22:20
Does /bin/sh support arrays?
â Jeff Schaller
Oct 20 '17 at 22:37
@Jesse_b I had tried it with both of those, previously, and it still did not work.
â Kimberlie Davis
Oct 20 '17 at 22:52
@JeffSchaller, I guess it depends on what OS is being used.
/bin/sh
is normally a symlink to bash though. @Kimberlie, try again. Your issue is thaturl
doesn't have a$
.â Jesse_b
Oct 20 '17 at 23:35