Reputation: 35
Execute the below command and got the output- command=
keytool -list -keystore /etc/pki/java/cacerts -storepass "changeit" | grep _abc_
output=
_abc_sec1, Mar 23, 2021, trustedCertEntry,
_abc_sec2, Aug 30, 2021, trustedCertEntry,
What is the easiest and short way to fetch _abc_sec1
and _abc_sec2
one by one and store in a variable
Upvotes: 0
Views: 303
Reputation: 44275
Using cut
to get the first column (separated by ,
)
We can use the arr=( $(command) )
syntax to parse the output to a bash array.
How do I assign the output of a command into an array?
Then we can easily loop over each substring:
#!/bin/bash
arr=( $(keytool -list -keystore /etc/pki/java/cacerts -storepass "changeit" | grep _abc_ | cut -d',' -f1) )
for ss in "${arr[@]}"; do
echo "--> $ss"
done
Local example, we're I've placed the 'input' in a text file to mimic the command:
$ cat input
_abc_sec1, Mar 23, 2021, trustedCertEntry,
_abc_sec2, Aug 30, 2021, trustedCertEntry,
something else
$
$
$ cat tst.sh
#!/bin/bash
arr=( $(cat input| grep _abc_ | cut -d',' -f1) )
for ss in "${arr[@]}"; do
echo "--> $ss"
done
$
$
$ ./tst.sh
--> _abc_sec1
--> _abc_sec2
$
Upvotes: 1