Bash how to loop through the output of a command and assign that output to variable [duplicate] - linux

This question already has answers here:
Looping through the content of a file in Bash
(16 answers)
Closed 11 months ago.
I have a for loop in my bash script.
for dir in $directories
do
dockerfile=$(find ./repo/$dir -name "Dockerfile");
docker build -f $dockerfile . -t $dir:version;
new_image=$(echo "$dir:version" | cut -d '/' -f2);
done
How can I pass $new_image output to another variable?
If I do something like this
new_image=$(echo "$image:version" | cut -d '/' -f2 >> list_images.txt)
I get the list of docker images in my .txt file but is there a way to pass the output to a variable? And update that variable value on each iteration?
The expected output after one iteration is something like
testing1:version
So when the for loop stops executing I get something like this in my .txt file.
testing1:version
kubernetes:version
node:version
And this is what I need, I can't figure out a better way to do this.

Using an answer, because the comments make it unreadable.
Do I understand correctly that you want:
for dir in $directories
do
dockerfile=$(find ./repo/$dir -name "Dockerfile");
docker build -f $dockerfile . -t $dir:version;
echo "$dir:version" | cut -d '/' -f2
done > list_images.txt
or do you actually have a need for the variable(s) further-on in your script?
And
new_image=$(echo "$dir:version")
trimmed=${new_image#*/}
will do your cut in bash, but that's just for bonuspoints :-)

Related

Piping into a part of bash command stored in variable [duplicate]

This question already has answers here:
Conditional step in a pipeline
(2 answers)
Can I make a shell function in as a pipeline conditionally "disappear", without using cat?
(1 answer)
Closed 4 months ago.
EMPTY_VAR=''
MMDDYYYY='6.18.1997'
PIPE_VAR=' | xargs echo "1+" | bc'
echo "$MMDDYYYY" | cut -d "." -f 2${EMPTY_VAR}
>> 18
Command above would give me correct output, which is 18, but if I try to use PIPE_VAR instead it would give me bunch of errors:
echo "$MMDDYYYY" | cut -d "." -f 2${PIPE_VAR}
cut: '|': No such file or directory
cut: xargs: No such file or directory
cut: echo: No such file or directory
cut: '"1+"': No such file or directory
cut: '|': No such file or directory
cut: bc: No such file or directory
OR:
echo "$MMDDYYYY" | cut -d "." -f 2"$PIPE_VAR"
cut: invalid field value ‘| xargs echo "1+" | bc’
Try 'cut --help' for more information.
What I'm really trying to find out is that even possible to combine commands like this?
You can't put control operators like | in a variable, at least not without resorting to something like eval. Syntax parsing comes before parameter expansion when evaluating the command line, so Bash is only ever going to see that | as a literal character and not pipeline syntax. See BashParsing for more details.
Conditionally adding a pipeline is hard to do well, but having a part of the pipeline conditionally execute one command or another is more straightforward. It might look something like this:
#!/bin/bash
MMDDYYYY='6.18.1997'
echo "$MMDDYYYY" | cut -d "." -f 2 |
if some_conditional_command ; then
xargs echo "1+" | bc
else
cat
fi
It looks like you're trying to calculate the next day. That's hard to do with plain arithmetic, particularly with month/year ends.
Let date do the work. This is GNU date. It can't parse 6.18.1997 but it can parse 6/18/1997
for MMDDYYYY in '2.28.1996' '2.28.1997'; do
date_with_slashes=${MMDDYYYY//./\/}
next_day=$(date -d "$date_with_slashes + 1 day" '+%-m.%-d.%Y')
echo "$next_day"
done
2.29.1996
3.1.1997

Execute script for all but certain files in directory [duplicate]

This question already has answers here:
How do I exclude a directory when using `find`?
(46 answers)
Closed 3 years ago.
I need a bash script to iterate on all files in directory besides one with specific names. Maybe it can be done with help of awk/sed during script execution?
Here is my script, that simply merge all file in directory to one:
#!/bin/bash
(find $DIR_NAME -name app.gz\* | sort -rV | xargs -L1 gunzip -c 2> /dev/null || :)
How can I add some $DIR_NAME to list, and don`t iterate over them?
Put the names of the files to be excluded into a file, say "blacklist.txt", one filename per line. Then use
... | grep -F -f blacklist.txt | sort ...
to exclude them from the input to xargs.

"Command not found" piping a variable to cut when output stored in a variable [duplicate]

This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 5 years ago.
In a bash script I am using a variable to hold a path like this:
MY_DIR=/just/a/string/to/my/path
And I want to remove the last two parts of it so it looks like this:
/just/a/string
I am using 'cut' to do it, like this:
echo $MY_DIR | cut -d'/' -f-4
The output is what I expect. Fine.
But I want to store in an other variable, like this:
MY_DIR2=$($MY_DIR | cut -d'/' -f-4)
When I execute the script I get the error:
... /just/a/string/to/my/path: No such file or directory
Why is the direct output with echo working, but storing the output in a variable is not?
You need to pass an input string to the shell command using a pipeline in which case cut or any standard shell commands, reads from stdin and acts on it. Some of the ways you can do this are use a pipe-line
dir2=$(echo "$MY_DIR" | cut -d'/' -f-4)
(or) use a here-string which is a shell built-in instead of launching a external shell process
dir2=$(cut -d'/' -f-4 <<< "$MY_DIR")
Use the grave accent(`) to emulate a command, and use echo too.
MY_DIR2=`echo $MY_DIR | cut -d'/' -f-4`

Read first characters of a variable in bash script? [duplicate]

This question already has answers here:
In Bash, how can I check if a string begins with some value?
(13 answers)
Closed 6 years ago.
I have a file where i get some informations through a bash script to put data in a DB table and i'd like to know how to read the first characters of a variable because if it starts with "CE-" that line's data will go into a table if not they must be inserted in an other one, how can i do this?
Like this-
var=CE-xxxxx
echo "$var"
output- CE-xxxxx
var2=$(echo "$var" | cut -c 1-3)
echo "$var2"
output- CE-
Then you can check if $var2 matches your criteria and use it further.
You can use cut to get the bytes that you need:
V="CE-IMPORTANT"
I=$(echo $V | cut -b 4-)
If you want to use the - as separator:
I=$(echo $V | cut -d '-' -f 2)
In both cases you get "IMPORTANT" in I var

linux bash tool to read property from property file [duplicate]

This question already has answers here:
How to grep for contents after pattern?
(8 answers)
Closed 6 years ago.
I want to be able to run a command to get a property from a property file. So if my properties file is property.props and has
username=dsollen
password=letMeIn
linux-skills=newb
I would like to have a quick way to pull out the property to pipe into other commands. so
./myProgram -v -p `getProp property.props password`
or something like that; not sure I used the ` right, but that's a different newb linux question for later :)
I know I can do this with a combination of grep and cut/awk/sed/whatever, but I'm wondering if there is an already existing tool that 'knows' how to read common property file formats and does something like this? If not I could write something to add into my scripts folder, just don't want to reinvent the wheel if a better wheel already exists.
You can use gvar to do the work. I'm the author by the way.
From the source code, this is the interesting part for you:
get_variable() {
< "$FILE" grep -w "$1" | cut -d'=' -f2
}
I assume your properties keys are unique, if so then the following might be a way:
grep -w "$1" <property.props
or to get the value
(grep -w "$1" | cut -d= -f2) <property.props
Where $1 is the key.
Look, just define in your .bashrc a function:
getProp() {
awk -F "=" "/^$2=/ {print "'$2'"; exit; }" $1
}
And use it in your shell:
$ getProp property.pros password
letMeIn
$ getProp property.pros linux-skills
newb

Resources