Saving lines from cat to array [duplicate] - linux

This question already has answers here:
Creating an array from a text file in Bash
(7 answers)
Closed 6 years ago.
I'm trying to read numbers from .txt file and than store it into array so I can sort them using bubble sort.
I was trying something like that:
input=$1
readIt=`cat $1`
array=${#readIt[*]}
When I was trying to display it using echo it is displaying good, but when I'm trying to sort it, then it doesn't work.
Any help, please?
EDIT: I checked other topics, but I want to solve this problem using "cat" to understand it in easier way as beginner.

Use readarray (bash 4+)
readarray -t array < "$1"
or a loop (prior to bash 4):
while IFS= read -r line; do
array+=("$line")
done < "$1"

Related

Writing a bash script to find all files in a directory that start with a, and do nothing if one exist [duplicate]

This question already has answers here:
Do not show results if directory is empty using Bash
(3 answers)
Closed 4 years ago.
So I have to find all the files in the directory that start with the letter a, and list them out. This is pretty easy by doing
cd some_directory
for file in a*; do
echo "$file"
done
However I want that if there are no files present that match a*, then the for loop will not run at all. Currently, if this is the case then the shell will echo
a*
Is there a way to do this? Thank you
Your text is opposite of your title, in my answer below I've assumed the text is your intention and your title is incorrect:
globs can be made to act like this with the bash shell option "nullglob":
shopt -s nullglob
An alternative is to use find and ignore errors by piping stderr to /dev/null
for file in $(find a* 2>/dev/null); do
echo "$file"
done

Bash echo weird behavior [duplicate]

This question already has answers here:
Variables overwriting text problem with "echo" in Bash
(2 answers)
Closed 4 years ago.
I wrote a script to change .CSV to json.
#!/bin/bash
exec 0< example.csv
while IFS=, read name element input decrease
do
echo "${element}decrease: ${decrease}test"
done
the example.csv I paste here
name1,A,11,12
name2,B,13,14
But the output is really weird...
testrease: 12
testrease: 14
As u can see, The test rewrite Adecrease and Bdecrease, makes them to testrease.
I can't believe it!! So I tried with out exec 0< example.csv, type them in stdin, this time I got what I want
name1,A,11,12
Adecrease: 12test
So I guess maybe there are some characters in example.csv I can't see which makes this problem. I use cat -v example.csv
name1,A,11,12^M
name2,B,13,14^M
Nothing strange and I stuck here.
I am very new to shell script, so if anyone can give me some suggestions I will be really thrilled!!
Thank u, #chepner! tag wiki saves me another hour on this stupid question.
And here is the solution from wiki:
Check whether your script or data has DOS style end-of-line characters.
Use cat -v yourfile or echo "$yourvariable" | cat -v.
DOS carriage returns will show up as ^M after each line.
If you find them, delete them using dos2unix (a.k.a. fromdos) or tr -d '\r'.

Concatenating multiple text files by arguments in a script into a single file in Bash [duplicate]

This question already has answers here:
How to cat multiple files from a list of files in Bash?
(6 answers)
Closed 4 years ago.
How should i concatenate multiple text files get by arguments in terminal using a script in Bash?
#!/bin/bash
while read $1
do
cat $1 > cat.txt
done
I tried that example but it is not working.
You should use '>>' to concatenate ('>' will create a new file each time):
for file in "$#"
do
cat $file >> result
done

Change file names iteratively in Linux [duplicate]

This question already has answers here:
Rename multiple files in bash
(3 answers)
Closed 8 years ago.
The only way I am aware of to do this operation is with a for loop iterating over each file:
for file in *something.txt; do
out=\`echo $file | sed 's/something/else/'\`; mv $file $out;
done
I was wondering if there is any other way or shortcut for it (using GNU bash).
There is also simple substring replacement provided as part of bash itself:
mv $file ${file/something/else}
example:
$ touch {1..3}something.txt
$ ( for i in *something.txt; do mv $i ${i/something/else}; done )
$ ls -1 *else*
1else.txt
2else.txt
3else.txt
There's rename and the same basic loop concept as in your post only in whatever programming language you choose.

How to store and echo multiple lines elegantly in bash? [duplicate]

This question already has answers here:
Capturing multiple line output into a Bash variable
(7 answers)
Closed 5 years ago.
I'm trying to capture a block of text into a variable, with newlines maintained, then echo it.
However, the newlines don't seemed to be maintained when I am either capturing the text or displaying it.
Any ideas regarding how I can accomplish this?
Example:
#!/bin/bash
read -d '' my_var <<"BLOCK"
this
is
a
test
BLOCK
echo $my_var
Output:
this is a test
Desired output:
this
is
a
test
You need to add " quotes around your variable.
echo "$my_var"

Resources