How do I make a variable recognized in a string variable?
I have the following bash script
#!/bin/bash
Sunday="
'$number'apples
'$number'oranges"
Monday="
'$number'bananas"
# ... you get my drift
Wednesday="
'$number'bananas
'$number'oranges"
# ... Until Saturday
range={1..3}
function GetDay()
{
if [ $(date +%A) == "Sunday" ]; then Day=$Sunday
elif [ $(date +%A) == "Monday" ]; then Day=$Monday
elif [ $(date +%A) == "Tuesday" ]; then Day=$Tuesday
elif [ $(date +%A) == "Wednesday" ]; then Day=$Wednesday
elif [ $(date +%A) == "Thursday" ]; then Day=$Thurday
elif [ $(date +%A) == "Friday" ]; then Day=$Friday
elif [ $(date +%A) == "Saturday" ]; then Day=$Saturday
fi
}
function CountFruits()
{
for number in $range
do
GetDay
for day in $Day
do
echo $day
done
done
}
Since its Wednesday I want it to output
1 bananas
1 oranges
2 bananas
2 oranges
3 bananas
3 oranges
How do I make this script work. It seems to be having troubles recognizing my range. It outputs
'' bananas
'' oranges
If I use the range directly with for number in {1..3}
instead of the variable reference for number in $range
'' bananas
'' oranges
'' bananas
'' oranges
'' bananas
'' oranges
string bash variables terminal gnu
add a comment |
I have the following bash script
#!/bin/bash
Sunday="
'$number'apples
'$number'oranges"
Monday="
'$number'bananas"
# ... you get my drift
Wednesday="
'$number'bananas
'$number'oranges"
# ... Until Saturday
range={1..3}
function GetDay()
{
if [ $(date +%A) == "Sunday" ]; then Day=$Sunday
elif [ $(date +%A) == "Monday" ]; then Day=$Monday
elif [ $(date +%A) == "Tuesday" ]; then Day=$Tuesday
elif [ $(date +%A) == "Wednesday" ]; then Day=$Wednesday
elif [ $(date +%A) == "Thursday" ]; then Day=$Thurday
elif [ $(date +%A) == "Friday" ]; then Day=$Friday
elif [ $(date +%A) == "Saturday" ]; then Day=$Saturday
fi
}
function CountFruits()
{
for number in $range
do
GetDay
for day in $Day
do
echo $day
done
done
}
Since its Wednesday I want it to output
1 bananas
1 oranges
2 bananas
2 oranges
3 bananas
3 oranges
How do I make this script work. It seems to be having troubles recognizing my range. It outputs
'' bananas
'' oranges
If I use the range directly with for number in {1..3}
instead of the variable reference for number in $range
'' bananas
'' oranges
'' bananas
'' oranges
'' bananas
'' oranges
string bash variables terminal gnu
2
You'll be more likely to get a response if you create a minimum viable concrete example of your question instead of posting your entire script. Also, try pasting your code into shellcheck.net to get some automatic suggestions of fixes.
– jeremysprofile
Nov 21 '18 at 14:06
@jeremysprofile actually I had tried to simplify my script. The real script is different from this. Here I was trying to represent my script. Thanks for the suggestion and I will check out the website
– Bret Joseph
Nov 22 '18 at 15:11
add a comment |
I have the following bash script
#!/bin/bash
Sunday="
'$number'apples
'$number'oranges"
Monday="
'$number'bananas"
# ... you get my drift
Wednesday="
'$number'bananas
'$number'oranges"
# ... Until Saturday
range={1..3}
function GetDay()
{
if [ $(date +%A) == "Sunday" ]; then Day=$Sunday
elif [ $(date +%A) == "Monday" ]; then Day=$Monday
elif [ $(date +%A) == "Tuesday" ]; then Day=$Tuesday
elif [ $(date +%A) == "Wednesday" ]; then Day=$Wednesday
elif [ $(date +%A) == "Thursday" ]; then Day=$Thurday
elif [ $(date +%A) == "Friday" ]; then Day=$Friday
elif [ $(date +%A) == "Saturday" ]; then Day=$Saturday
fi
}
function CountFruits()
{
for number in $range
do
GetDay
for day in $Day
do
echo $day
done
done
}
Since its Wednesday I want it to output
1 bananas
1 oranges
2 bananas
2 oranges
3 bananas
3 oranges
How do I make this script work. It seems to be having troubles recognizing my range. It outputs
'' bananas
'' oranges
If I use the range directly with for number in {1..3}
instead of the variable reference for number in $range
'' bananas
'' oranges
'' bananas
'' oranges
'' bananas
'' oranges
string bash variables terminal gnu
I have the following bash script
#!/bin/bash
Sunday="
'$number'apples
'$number'oranges"
Monday="
'$number'bananas"
# ... you get my drift
Wednesday="
'$number'bananas
'$number'oranges"
# ... Until Saturday
range={1..3}
function GetDay()
{
if [ $(date +%A) == "Sunday" ]; then Day=$Sunday
elif [ $(date +%A) == "Monday" ]; then Day=$Monday
elif [ $(date +%A) == "Tuesday" ]; then Day=$Tuesday
elif [ $(date +%A) == "Wednesday" ]; then Day=$Wednesday
elif [ $(date +%A) == "Thursday" ]; then Day=$Thurday
elif [ $(date +%A) == "Friday" ]; then Day=$Friday
elif [ $(date +%A) == "Saturday" ]; then Day=$Saturday
fi
}
function CountFruits()
{
for number in $range
do
GetDay
for day in $Day
do
echo $day
done
done
}
Since its Wednesday I want it to output
1 bananas
1 oranges
2 bananas
2 oranges
3 bananas
3 oranges
How do I make this script work. It seems to be having troubles recognizing my range. It outputs
'' bananas
'' oranges
If I use the range directly with for number in {1..3}
instead of the variable reference for number in $range
'' bananas
'' oranges
'' bananas
'' oranges
'' bananas
'' oranges
string bash variables terminal gnu
string bash variables terminal gnu
asked Nov 21 '18 at 13:07
Bret Joseph
3410
3410
2
You'll be more likely to get a response if you create a minimum viable concrete example of your question instead of posting your entire script. Also, try pasting your code into shellcheck.net to get some automatic suggestions of fixes.
– jeremysprofile
Nov 21 '18 at 14:06
@jeremysprofile actually I had tried to simplify my script. The real script is different from this. Here I was trying to represent my script. Thanks for the suggestion and I will check out the website
– Bret Joseph
Nov 22 '18 at 15:11
add a comment |
2
You'll be more likely to get a response if you create a minimum viable concrete example of your question instead of posting your entire script. Also, try pasting your code into shellcheck.net to get some automatic suggestions of fixes.
– jeremysprofile
Nov 21 '18 at 14:06
@jeremysprofile actually I had tried to simplify my script. The real script is different from this. Here I was trying to represent my script. Thanks for the suggestion and I will check out the website
– Bret Joseph
Nov 22 '18 at 15:11
2
2
You'll be more likely to get a response if you create a minimum viable concrete example of your question instead of posting your entire script. Also, try pasting your code into shellcheck.net to get some automatic suggestions of fixes.
– jeremysprofile
Nov 21 '18 at 14:06
You'll be more likely to get a response if you create a minimum viable concrete example of your question instead of posting your entire script. Also, try pasting your code into shellcheck.net to get some automatic suggestions of fixes.
– jeremysprofile
Nov 21 '18 at 14:06
@jeremysprofile actually I had tried to simplify my script. The real script is different from this. Here I was trying to represent my script. Thanks for the suggestion and I will check out the website
– Bret Joseph
Nov 22 '18 at 15:11
@jeremysprofile actually I had tried to simplify my script. The real script is different from this. Here I was trying to represent my script. Thanks for the suggestion and I will check out the website
– Bret Joseph
Nov 22 '18 at 15:11
add a comment |
1 Answer
1
active
oldest
votes
The immediate problem is that $number
in each of your day variables is expanded immediately, so the value of Sunday
is actually
'' apples
'' oranges
You would need to use single quotes on the outside to prevent expansion; the inner quotes are just regular characters.
However, this won't work, because the string $number
inside the variable is just literal text as well; it won't be expanded with the current value of $number
when $Day
or $day
expands.
What you need is a function:
dailyList () {
# $1 - day of the week
# $2 - a number
case $1 in
Sunday) fruits=(apples oranges) ;;
Monday) fruits=(bananas) ;;
# etc
esac
for f in "${fruits[@]}"; do
printf '%d %s' "$2" "$f"
done
}
Then, call that from your loop:
CountFruits () {
for number in "$@"
do
dailyList "$(date +%A)" "$number"
done
}
CountFruits {1..3}
I love the way you put the code. The only problem with this would be that I actually wanted the code to be likefruits=('x' apples)
, in case I want to append the x in an array, like iffruits=('x' apples and 'x' oranges)
. Like you said if I put quotes it will make'x' apples and 'x' oranges'
text which was my problem. So it seems there is no way around not making the array string text without using quotes
– Bret Joseph
Nov 21 '18 at 15:42
That's different from what you asked for in the question. Would updating the call toprintf
be sufficient?
– chepner
Nov 21 '18 at 17:30
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412754%2fhow-do-i-make-a-variable-recognized-in-a-string-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The immediate problem is that $number
in each of your day variables is expanded immediately, so the value of Sunday
is actually
'' apples
'' oranges
You would need to use single quotes on the outside to prevent expansion; the inner quotes are just regular characters.
However, this won't work, because the string $number
inside the variable is just literal text as well; it won't be expanded with the current value of $number
when $Day
or $day
expands.
What you need is a function:
dailyList () {
# $1 - day of the week
# $2 - a number
case $1 in
Sunday) fruits=(apples oranges) ;;
Monday) fruits=(bananas) ;;
# etc
esac
for f in "${fruits[@]}"; do
printf '%d %s' "$2" "$f"
done
}
Then, call that from your loop:
CountFruits () {
for number in "$@"
do
dailyList "$(date +%A)" "$number"
done
}
CountFruits {1..3}
I love the way you put the code. The only problem with this would be that I actually wanted the code to be likefruits=('x' apples)
, in case I want to append the x in an array, like iffruits=('x' apples and 'x' oranges)
. Like you said if I put quotes it will make'x' apples and 'x' oranges'
text which was my problem. So it seems there is no way around not making the array string text without using quotes
– Bret Joseph
Nov 21 '18 at 15:42
That's different from what you asked for in the question. Would updating the call toprintf
be sufficient?
– chepner
Nov 21 '18 at 17:30
add a comment |
The immediate problem is that $number
in each of your day variables is expanded immediately, so the value of Sunday
is actually
'' apples
'' oranges
You would need to use single quotes on the outside to prevent expansion; the inner quotes are just regular characters.
However, this won't work, because the string $number
inside the variable is just literal text as well; it won't be expanded with the current value of $number
when $Day
or $day
expands.
What you need is a function:
dailyList () {
# $1 - day of the week
# $2 - a number
case $1 in
Sunday) fruits=(apples oranges) ;;
Monday) fruits=(bananas) ;;
# etc
esac
for f in "${fruits[@]}"; do
printf '%d %s' "$2" "$f"
done
}
Then, call that from your loop:
CountFruits () {
for number in "$@"
do
dailyList "$(date +%A)" "$number"
done
}
CountFruits {1..3}
I love the way you put the code. The only problem with this would be that I actually wanted the code to be likefruits=('x' apples)
, in case I want to append the x in an array, like iffruits=('x' apples and 'x' oranges)
. Like you said if I put quotes it will make'x' apples and 'x' oranges'
text which was my problem. So it seems there is no way around not making the array string text without using quotes
– Bret Joseph
Nov 21 '18 at 15:42
That's different from what you asked for in the question. Would updating the call toprintf
be sufficient?
– chepner
Nov 21 '18 at 17:30
add a comment |
The immediate problem is that $number
in each of your day variables is expanded immediately, so the value of Sunday
is actually
'' apples
'' oranges
You would need to use single quotes on the outside to prevent expansion; the inner quotes are just regular characters.
However, this won't work, because the string $number
inside the variable is just literal text as well; it won't be expanded with the current value of $number
when $Day
or $day
expands.
What you need is a function:
dailyList () {
# $1 - day of the week
# $2 - a number
case $1 in
Sunday) fruits=(apples oranges) ;;
Monday) fruits=(bananas) ;;
# etc
esac
for f in "${fruits[@]}"; do
printf '%d %s' "$2" "$f"
done
}
Then, call that from your loop:
CountFruits () {
for number in "$@"
do
dailyList "$(date +%A)" "$number"
done
}
CountFruits {1..3}
The immediate problem is that $number
in each of your day variables is expanded immediately, so the value of Sunday
is actually
'' apples
'' oranges
You would need to use single quotes on the outside to prevent expansion; the inner quotes are just regular characters.
However, this won't work, because the string $number
inside the variable is just literal text as well; it won't be expanded with the current value of $number
when $Day
or $day
expands.
What you need is a function:
dailyList () {
# $1 - day of the week
# $2 - a number
case $1 in
Sunday) fruits=(apples oranges) ;;
Monday) fruits=(bananas) ;;
# etc
esac
for f in "${fruits[@]}"; do
printf '%d %s' "$2" "$f"
done
}
Then, call that from your loop:
CountFruits () {
for number in "$@"
do
dailyList "$(date +%A)" "$number"
done
}
CountFruits {1..3}
answered Nov 21 '18 at 14:07
chepner
245k32232323
245k32232323
I love the way you put the code. The only problem with this would be that I actually wanted the code to be likefruits=('x' apples)
, in case I want to append the x in an array, like iffruits=('x' apples and 'x' oranges)
. Like you said if I put quotes it will make'x' apples and 'x' oranges'
text which was my problem. So it seems there is no way around not making the array string text without using quotes
– Bret Joseph
Nov 21 '18 at 15:42
That's different from what you asked for in the question. Would updating the call toprintf
be sufficient?
– chepner
Nov 21 '18 at 17:30
add a comment |
I love the way you put the code. The only problem with this would be that I actually wanted the code to be likefruits=('x' apples)
, in case I want to append the x in an array, like iffruits=('x' apples and 'x' oranges)
. Like you said if I put quotes it will make'x' apples and 'x' oranges'
text which was my problem. So it seems there is no way around not making the array string text without using quotes
– Bret Joseph
Nov 21 '18 at 15:42
That's different from what you asked for in the question. Would updating the call toprintf
be sufficient?
– chepner
Nov 21 '18 at 17:30
I love the way you put the code. The only problem with this would be that I actually wanted the code to be like
fruits=('x' apples)
, in case I want to append the x in an array, like if fruits=('x' apples and 'x' oranges)
. Like you said if I put quotes it will make 'x' apples and 'x' oranges'
text which was my problem. So it seems there is no way around not making the array string text without using quotes– Bret Joseph
Nov 21 '18 at 15:42
I love the way you put the code. The only problem with this would be that I actually wanted the code to be like
fruits=('x' apples)
, in case I want to append the x in an array, like if fruits=('x' apples and 'x' oranges)
. Like you said if I put quotes it will make 'x' apples and 'x' oranges'
text which was my problem. So it seems there is no way around not making the array string text without using quotes– Bret Joseph
Nov 21 '18 at 15:42
That's different from what you asked for in the question. Would updating the call to
printf
be sufficient?– chepner
Nov 21 '18 at 17:30
That's different from what you asked for in the question. Would updating the call to
printf
be sufficient?– chepner
Nov 21 '18 at 17:30
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53412754%2fhow-do-i-make-a-variable-recognized-in-a-string-variable%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
You'll be more likely to get a response if you create a minimum viable concrete example of your question instead of posting your entire script. Also, try pasting your code into shellcheck.net to get some automatic suggestions of fixes.
– jeremysprofile
Nov 21 '18 at 14:06
@jeremysprofile actually I had tried to simplify my script. The real script is different from this. Here I was trying to represent my script. Thanks for the suggestion and I will check out the website
– Bret Joseph
Nov 22 '18 at 15:11