Missing File In Bash - Create a file and assign it to a variable

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-1
down vote

favorite












I'm trying to create a file and assign it to a variable with these lines:



aws_key="company-lab"
source_dir="source_files"
aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt)


But when I go to use the variable "$aws_role_list" it's empty:



echo "echo the file"
echo "$aws_role_list"


Running the script with bash -x gives you:



+ aws_role_list=


And listing the file like this:



echo "listing the file"
ls -lh "$aws_role_list"


Gives you:



+ ls -lh ''
ls: cannot access '': No such file or directory


What am I doing wrong? How can I use the aws_role_list variable correctly?










share|improve this question





















  • The question is not how to use aws_role_list variable correctly. It is what is causing the variable to not have any value. Decompose the command and see where things fail.
    – Lewis M
    10 hours ago














up vote
-1
down vote

favorite












I'm trying to create a file and assign it to a variable with these lines:



aws_key="company-lab"
source_dir="source_files"
aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt)


But when I go to use the variable "$aws_role_list" it's empty:



echo "echo the file"
echo "$aws_role_list"


Running the script with bash -x gives you:



+ aws_role_list=


And listing the file like this:



echo "listing the file"
ls -lh "$aws_role_list"


Gives you:



+ ls -lh ''
ls: cannot access '': No such file or directory


What am I doing wrong? How can I use the aws_role_list variable correctly?










share|improve this question





















  • The question is not how to use aws_role_list variable correctly. It is what is causing the variable to not have any value. Decompose the command and see where things fail.
    – Lewis M
    10 hours ago












up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm trying to create a file and assign it to a variable with these lines:



aws_key="company-lab"
source_dir="source_files"
aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt)


But when I go to use the variable "$aws_role_list" it's empty:



echo "echo the file"
echo "$aws_role_list"


Running the script with bash -x gives you:



+ aws_role_list=


And listing the file like this:



echo "listing the file"
ls -lh "$aws_role_list"


Gives you:



+ ls -lh ''
ls: cannot access '': No such file or directory


What am I doing wrong? How can I use the aws_role_list variable correctly?










share|improve this question













I'm trying to create a file and assign it to a variable with these lines:



aws_key="company-lab"
source_dir="source_files"
aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt)


But when I go to use the variable "$aws_role_list" it's empty:



echo "echo the file"
echo "$aws_role_list"


Running the script with bash -x gives you:



+ aws_role_list=


And listing the file like this:



echo "listing the file"
ls -lh "$aws_role_list"


Gives you:



+ ls -lh ''
ls: cannot access '': No such file or directory


What am I doing wrong? How can I use the aws_role_list variable correctly?







bash shell-script






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 10 hours ago









user99201

1095




1095











  • The question is not how to use aws_role_list variable correctly. It is what is causing the variable to not have any value. Decompose the command and see where things fail.
    – Lewis M
    10 hours ago
















  • The question is not how to use aws_role_list variable correctly. It is what is causing the variable to not have any value. Decompose the command and see where things fail.
    – Lewis M
    10 hours ago















The question is not how to use aws_role_list variable correctly. It is what is causing the variable to not have any value. Decompose the command and see where things fail.
– Lewis M
10 hours ago




The question is not how to use aws_role_list variable correctly. It is what is causing the variable to not have any value. Decompose the command and see where things fail.
– Lewis M
10 hours ago










1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










I have no experience with AWS, but I can see that you are redirecting a command's output to a file:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt


Since the output is going into a file, when you use var=$(command), it is reasonable that var will be empty because command doesn't return anything: it's all going to "$source_dir"/aws-"$aws_key"-role-list.txt.



So, you either want this:



aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName')


Or this:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt
aws_role_list=$(cat "$source_dir"/aws-"$aws_key"-role-list.txt)


If you are trying to get the file's name and not its contents into the variable, then you want this:



aws_key="company-lab"
source_dir="source_files"
aws_role_list="$source_dir"/aws-"$aws_key"-role-list.txt
aws iam list-roles --profile="$aws_key" |
jq -r '.Roles.RoleName' > "$aws_role_list"





share|improve this answer






















  • Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
    – user99201
    10 hours ago










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%2f474256%2fmissing-file-in-bash-create-a-file-and-assign-it-to-a-variable%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
3
down vote



accepted










I have no experience with AWS, but I can see that you are redirecting a command's output to a file:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt


Since the output is going into a file, when you use var=$(command), it is reasonable that var will be empty because command doesn't return anything: it's all going to "$source_dir"/aws-"$aws_key"-role-list.txt.



So, you either want this:



aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName')


Or this:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt
aws_role_list=$(cat "$source_dir"/aws-"$aws_key"-role-list.txt)


If you are trying to get the file's name and not its contents into the variable, then you want this:



aws_key="company-lab"
source_dir="source_files"
aws_role_list="$source_dir"/aws-"$aws_key"-role-list.txt
aws iam list-roles --profile="$aws_key" |
jq -r '.Roles.RoleName' > "$aws_role_list"





share|improve this answer






















  • Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
    – user99201
    10 hours ago














up vote
3
down vote



accepted










I have no experience with AWS, but I can see that you are redirecting a command's output to a file:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt


Since the output is going into a file, when you use var=$(command), it is reasonable that var will be empty because command doesn't return anything: it's all going to "$source_dir"/aws-"$aws_key"-role-list.txt.



So, you either want this:



aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName')


Or this:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt
aws_role_list=$(cat "$source_dir"/aws-"$aws_key"-role-list.txt)


If you are trying to get the file's name and not its contents into the variable, then you want this:



aws_key="company-lab"
source_dir="source_files"
aws_role_list="$source_dir"/aws-"$aws_key"-role-list.txt
aws iam list-roles --profile="$aws_key" |
jq -r '.Roles.RoleName' > "$aws_role_list"





share|improve this answer






















  • Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
    – user99201
    10 hours ago












up vote
3
down vote



accepted







up vote
3
down vote



accepted






I have no experience with AWS, but I can see that you are redirecting a command's output to a file:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt


Since the output is going into a file, when you use var=$(command), it is reasonable that var will be empty because command doesn't return anything: it's all going to "$source_dir"/aws-"$aws_key"-role-list.txt.



So, you either want this:



aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName')


Or this:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt
aws_role_list=$(cat "$source_dir"/aws-"$aws_key"-role-list.txt)


If you are trying to get the file's name and not its contents into the variable, then you want this:



aws_key="company-lab"
source_dir="source_files"
aws_role_list="$source_dir"/aws-"$aws_key"-role-list.txt
aws iam list-roles --profile="$aws_key" |
jq -r '.Roles.RoleName' > "$aws_role_list"





share|improve this answer














I have no experience with AWS, but I can see that you are redirecting a command's output to a file:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt


Since the output is going into a file, when you use var=$(command), it is reasonable that var will be empty because command doesn't return anything: it's all going to "$source_dir"/aws-"$aws_key"-role-list.txt.



So, you either want this:



aws_role_list=$(aws iam list-roles --profile="$aws_key" | jq -r '.Roles.RoleName')


Or this:



aws iam list-roles --profile="$aws_key" | 
jq -r '.Roles.RoleName' > "$source_dir"/aws-"$aws_key"-role-list.txt
aws_role_list=$(cat "$source_dir"/aws-"$aws_key"-role-list.txt)


If you are trying to get the file's name and not its contents into the variable, then you want this:



aws_key="company-lab"
source_dir="source_files"
aws_role_list="$source_dir"/aws-"$aws_key"-role-list.txt
aws iam list-roles --profile="$aws_key" |
jq -r '.Roles.RoleName' > "$aws_role_list"






share|improve this answer














share|improve this answer



share|improve this answer








edited 10 hours ago

























answered 10 hours ago









terdon♦

124k29234409




124k29234409











  • Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
    – user99201
    10 hours ago
















  • Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
    – user99201
    10 hours ago















Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
– user99201
10 hours ago




Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
– user99201
10 hours ago

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f474256%2fmissing-file-in-bash-create-a-file-and-assign-it-to-a-variable%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