Missing File In Bash - Create a file and assign it to a variable
Clash 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?
bash shell-script
add a comment |Â
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?
bash shell-script
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
add a comment |Â
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?
bash shell-script
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
bash shell-script
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
add a comment |Â
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
add a comment |Â
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"
Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
â user99201
10 hours ago
add a comment |Â
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"
Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
â user99201
10 hours ago
add a comment |Â
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"
Thanks! That makes perfect sense, and it works. I appreciate your valuable input.
â user99201
10 hours ago
add a comment |Â
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"
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"
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
add a comment |Â
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
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%2f474256%2fmissing-file-in-bash-create-a-file-and-assign-it-to-a-variable%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
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