This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Bash arbitrary glob pattern (with spaces) in for loop
(2 answers)
Closed 2 years ago.
I'm trying to read a bash array file by file but for files with spaces in them, it comes out as two separate files. How would I solve this?
file names:
file A
fileB
file_C
the output Im getting is like this
File
A
FileB
File_C
What Im trying to do is to get File A on one line
I know I can read the array at once but for now we need to do it with a for loop since we're gonna be doing operations to it later in the loop.
for i in "${array[#]}"; do
echo "$i"
done
Related
This question already has answers here:
How can I join elements of an array in Bash?
(34 answers)
Closed 3 years ago.
How can I control how an array separates its elements? If you do:
echo ${array[*]}
it displays:
element1 element2 element3
and I would like it to be:
element1:element2:element3
Elements are joined by the first character of $IFS (internal field separator).
(IFS=':'; echo "${array[*]}")
Modifying $IFS has a lot of side effects. I recommend only changing it for a short duration inside a subshell so the rest of your script isn't affected.
This question already has answers here:
Printing lines from a text file in reverse order
(2 answers)
Closed 5 years ago.
Im trying to read a file not just from the last line but from the end of the line to the beginning too. Example:
Im trying
to read a file
How do I want to read:
elif a daer to
gniyrt mI
How do I do that?
You read each line, put all in a buffer ..reverse it and then parse.
The thing is most of the time it is not needed to read the file in reversed manner..But you can try this.
Some nice organized manner of doing this will be..using a LIFO data structure.
You can use a simple array and make it behave like a stack. Push everything then pop from top.
This question already has answers here:
Accessing bash command line args $# vs $*
(5 answers)
Closed 6 years ago.
x=('hello world' "HELLO")
Both ${#x[*]} and ${#x[#]} print the same output.
I understand the difference between $# and $* but I am interested to see the difference without command line arguments.
Always use # expansion unless you have reason to use *. # was added to work around a problem.
The two don't ALWAYS expand the same. The troubles involving* start with spaces and other shell metacharacters (quotes in particular, but $ and more as well).
The * leaves the metacharacters open for the shell to process them again, which is usually bad if you went out of your way to get them into the array. The # protects them by expanding each array element as if it was a separately quoted value, leaving all metacharacters intact.
This question already has answers here:
Multi-dimensional arrays in Bash
(13 answers)
Closed 9 years ago.
i have a script which get datas from a file, but some parameters have more values than just one.
How can i make that my bash script can read as example:
user[3][1]=test1
user[3][2]=test2
Actually i make:
for i in $(seq ${#lala[#]}); do
${user[$i]}
done
which works. I have try it with ${user[$i][$i]} and seq ${#lala[#][#]}) but i cant get it running.
The script must then work with just [$i] and [$i][$i].
Can please somebody help me out, thanks a lot!
Well you're using wrong language/script, since BASH doesn't support multi-dimensional arrays. Having said that you can store a delimited text in each element of array that you can further break into array inside a loop.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Delete a Line from a file in C Language
I know we could overwrite the first line, but here i need to total remove it.
For example, the file contains 100 lines, after i remove the first line, there are 99 lines left and the file size if reduced.
Note that the file is pretty large.
I don't know how to start this, is there any suggestion ? many thanks !
collect all the text of your file into a buffer and copy the same back into yoour file and whenever the required line appears, skip it.. This is all you can do..