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
This question already has answers here:
Convert integers to strings to create output filenames at run time
(9 answers)
Closed 2 years ago.
As there are lots of folders and files, I wish to use loop to open the folder and read them.
The names of the folders to open and read them are rt20, rt30, rt40 and so on. Whereas, the names of the folders to save some text files are sphere20, sphere30, etc.
I created an array of characters and concatenate with the name of the folders but failed.
The actual code that I created is:
PROGRAM TEST1
IMPLICIT NONE
INTEGER ::lskip,lread,count
INTEGER :: n,I,j
REAL l,b,c,d,e,ReThe,ImThe,RePhi,ImPhi
CHARACTER(len=100) :: FN,FNL
character*2,parameter :: A(*) = ['20','30','40','50','60','70']
N=256
do j = 1,6
OPEN(11,file='rt',A(j),'\\output.dat',form='formatted')
!--allocate your vectors here
DO I=1,N
WRITE(FN,10)I
WRITE(6,*)FN
OPEN(1,FILE=FN)
!skip first 17 lines
do lskip = 1,18
READ(1,*)
end do
! Now read actual lines
do lread = 1,1
READ(1,*)l,b,ReThe,ImThe,RePhi,ImPhi,c,d,e
end do
write(11,20)ReThe,ImThe,RePhi,ImPhi
CLOSE(1)
END DO
10 format ('pm\\vertical\\sphere',A(j),'\\n_FarField',I0,'.ffe')
20 format (E14.7,4X,E14.7,4X,E14.7,4X,E14.7)
end do
END PROGRAM
The errors that are appeared in the program:
In file C:\Users\Hamsalekha\Desktop\far_field_sphere\Source1.f:12
OPEN(11,file='rt',A(j),'\\output.dat',form='formatted')
1
Error: Syntax error in OPEN statement at (1)
In file C:\Users\Hamsalekha\Desktop\far_field_sphere\Source1.f:33
10 FORMAT('pm\\vertical\\sphere',A(j),'\\n_FarField',I0,'.ffe')
1
Error: Unexpected element in format string at (1)
I couldn't find a solution on any websites. I really appreciate if someone can help me to solve this problem or give any hint.
You are concatenating your strings incorrectly:
OPEN(11,file='rt',A(j),'\\output.dat',form='formatted')
This only asks for file "rt" and than adds some other strange arguments. You need
OPEN(11,file='rt'//A(j)//'/output.dat',form='formatted')
Be aware that \ does not work as an escaping character in Fortran strings and that / is the proper multiplatform character to separate directories in the hierarchy. Yes, even in MS Windows.
The // is the Fortran string concatenation operator.
Jean-Claude Arbaut mentions in the comment the reason for the other error. One needs a constant string in the FORMAT statement. I personally strongly prefer to use format strings in the write statements directly and also to put what I can in the output list:
WRITE(FN,'(3A,I0,A)') 'pm/vertical/sphere',A(j),'/n_FarField',I,'.ffe'
but there are many other ways to write it. You can concatenate the string. You can also use the FORMAT statement.
WRITE(FN,10) A(j), I
10 FORMAT('pm/vertical/sphere',A,'/n_FarField',I0,'.ffe')
Many examples can be found here: Convert integers to strings to create output filenames at run time
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 an answer here:
Closed 11 years ago.
Possible Duplicate:
tcl array question - key with quotes
I have the following code:
set my_list1 {"a" "b"}
set my_list2 {"#1" "#2"}
array set my_array {}
foreach li1 $my_list1 li2 $my_list2 {
set my_array($li1) $li2
}
puts $my_array("a")
On the list line I get ERROR "can't read my_array("a"): no such element in array"
Why?
I have it, because when I write
set newVar "a"
puts $my_array($newVar)
it returns the value!
This is just one of those things in Tcl. The array element is not my_array("a") -- it's my_array(a). Don't include the quotes when referencing the array. They're actually not necessary, although in that case note harmful, when you're installing the data into the array in the first place -- i.e.,
set my_list1 {a b}
would be just fine.
Tcl looks enough like a "normal" programming language that it's easy to forget how primitive its parser really is. Remember that everything is broken down into "words" by whitespace. If a double-quote character isn't preceded by whitespace, it isn't at the start of a word, and it no longer has any special significance. A reference to an array element is a single word, and after variable interpolation, it has to have exactly the right text. You can't put quote marks around the element name because simply those quote marks are not part of the correct text of that word.