Appending output from a command to a variable in Bash - bash

I'm trying to append an output of a command to a variable in Bash. My code is
#!/bin/bash
for file in *
do
lineInfo=`wc -l $file`
echo "$lineInfo"
done
I understand how to "capture" the output of a command to a variable as I have done in this line by the use of backquotes.
lineInfo=`wc -l $file`
Is there a clean cut way I can place the output of that entire for loop into a variable in Bash? Or in each iteration of the for loop append the output of the wc command to linesInfo ? (Without redirecting anything to files) Thanks.

This stores all the line infos (separated by commas) into one variable and prints that variable:
#!/bin/bash
total=""
for file in *
do
lineInfo=`wc -l $file`
total="$total$lineInfo, " # or total+="$lineInfo, "
done
echo $total

Related

Store multiline eval output in variable bash

Can someone help me to store multiline output in variable in bash. I have the following code:
# FILES[1] contents:
# age people.csv
# ...data...
FILE="${FILES[1]}"
cmd=$(head -n 1 "$FILE")
cmd="./corona $cmd"
echo "Command to run: ${cmd[*]}"
output=$(eval "$cmd")
echo "$output"
I'm trying to store the output of corona script in output variable. But it doesn't seem to work. The output stucks at
Command to run: ./corona age people.csv
And on the second line I can see only the blinking cursor. But when I press Ctrl+D it suddenly prints all the output from corona script and stops. So, probably, the echo command works just after pressing the shortcut.
Also, I'd like to mention, that variable FILES is an array of filenames. So the FILE variable is a name of the file. It has command arguments to run on the first line and other data starting from the second line.
Here is a sample script I developed to read the output of ls into a variable. You could use the same technique.
#!/bin/bash
my_array=()
while IFS= read -r line; do
my_array+=( "$line" )
done < <( ls )
echo ${#my_array[#]}
printf '%s\n' "${my_array[#]}"

Bash access for loops variable via command passed as argument

I have a frequent situation where I want to run a given command over all files that match a certain pattern. As such I have the following:
iterate.sh
#!/bin/bash
for file in $1; do
$2
done
So I can use it as such iterate.sh "*.png" "echo $file"
The problem is when the command is being ran it doesn't seem to have access to the $file variable as that sample command will just output a blank line for every file.
How can I reference the iterator $file from the arguments of the program?
#!/bin/bash
for file in $1; do
eval $2
done
iterate.sh "*.png" 'echo $file'
Need single quotes around the argument with $file so it doesn't expand on the command line. Need to eval in the loop to actually do the command in the argument instead of just expanding the argument.

How can I log/read a string in a bash script without executing it?

I'm starting to learn how to write shell scripts. I have them all placed in a folder 'personal-scripts' in my home directory. They are starting to add up though. To solve this, I am attempting to create a script that loops over the directory and gives me a brief sentence about what each script does.
For now I can only output the script location and names via:
scriptsinfo
#!/bin/bash
for filename in ~/personal-scripts/*
do
echo $filename
done
Since I don't want to go back and update this file manually, I want to place the about sentence inside each other script either as a comment or string variable that can be evaluated.
How can I read the contents of each other script in the folder and output their string/info about what they do in this script?
You can do that using head command, which prints the first n lines of a file.
test.sh
# this is about line
# date is 14-9-2017
script data
..
..
~# head -n 2 test.sh
# this is about line
# date is 14-9-2017
If you add the description on each second line of your script, (after #!/bin/bash then let use sed -n "2p" $filenamein your script. You can also add a separator between each script:
#!/bin/bash
for filename in ~/personal-scripts/*
do
echo "---------------------"
echo $filename
echo "---------------------"
sed -n "2p" $filename
done
The alternative is to put the description anywhere, in a line starting by e.g # about: then you can grep it:
#!/bin/bash
for filename in ~/personal-scripts/*
do
echo "---------------------"
echo $filename
echo "---------------------"
grep "# about:" $filename | sed 's/# about://'
done
The | sed 's/# about://' is there to keep the description only.
If you have a comment inside all your scripts with a specific pattern such as
#info : This script runs daily
Then you can simply grep for the pattern and append to the name of the script in each line.
for filename in ~/personal-scripts/*
do
echo "$i : $(grep '#info' $i)"
done

Bash Script that Creates a File with a Variable in the Filename

I am trying to write a variable into a file and also use the variable as the name of the file itself. Any ideas?
EXAMPLE 1: Filename and content within that file should be "helloworld"
#/bin/bash
OUTPUT="helloworld"
echo $OUTPUT > ~/Desktop/directory/outputs/$OUTPUT.txt
EXAMPLE 2: Filename and content within file should be "hellokitty"
#/bin/bash
OUTPUT="hellokitty"
echo $OUTPUT > ~/Desktop/directory/outputs/$OUTPUT.txt
This should work
#!/usr/bin/env bash
OUTPUT=hellokitty
echo "$OUTPUT" > ~/Desktop/directory/outputs/"${OUTPUT}".txt
You need to tell bash where your variable starts and where it ends. You can do this by using "${OUTPUT}" instead of just "$OUTPUT".
If you really want to do it that way, just use a 2-step, e.g.:
#!/bin/bash
output="helloword"
filename="$HOME/Desktop/directory/outputs/${output}".txt
echo "$output" > "$filename"

Bash: Error when storing result of unix command to variable in while loop

I have the following script called test.sh:
echo "file path is : $1"
path=$1
while read -r line
do
num=$($line | tr -cd [:digit:])
echo num
done < $path
exit 0
I am attempting to grab the digit at the start of each line of the file stored as $path. the end result will be to loop over each line, grab the digit and remove it from the file if it is less than 2.
Every time i run this loop i get the error "./test.sh: line 5: : command not found. What part of the while loop am I doing wrong? Or is it something to do with the tr command?
I can spot a few things wrong with your script:
#!/bin/bash
echo "file path is : $1"
path=$1
while read -r line
do
num=$(tr -cd '[:digit:]' <<<"$line") # use here string to "echo" variable to tr
echo "$num" # added quotes and $
done < "$path" # added quotes, changed $dest to $path
In summary:
cmd <<<"$var" (here string) is a bash built-in designed as a replacement for echo "$var" | cmd. I added #!/bin/bash to the top of the script, as I am using this bash-only feature.
I have quoted your variables to prevent problems with word splitting and glob expansion.
I made the assumption that you really meant to use $path on the last line (though I may be wrong).
Finally, there's no need to exit 0 at the end of your script.

Resources