I'm new to coding and am trying to iterate through folders to find a specific file (called the same thing in each folder). I have 3 folders (CONTROL, GROUP1, and GROUP2). Each folder has 2 subfolders in it from the folder names (2 3 4 5 6 7. Each subfolder has a file in it with the subfolder name such as 2_diff.nii or 3_diff.nii. I'd like the code to go into each folder, find the subfolders, and then perform an analysis of the file in there. The problem is that my code seems to be looking for each subfolder in each main folder and each main folder only has 2 of the subfolders out of (2 3 4 5 6 7). Any tips would be greatly appreciated - thank you!!
Folders=(CONTROL GROUP1 GROUP2)
SubFolders=(2 3 4 5 6 7)
data_source=/Users/sheena/Desktop/test/
for j in ${Folders[#]}; do
cd ${data_source}/${j}/
for i in ${SubFolders[#]}; do
fslroi ${i}_diff.nii ${i}_nodif 0 1 #I want to analyze the file <subfolder>_diff.nii and name the output as <subfolder>_nodif.nii
done
done
The way I understand your question is that in each of the directories CONTROL, GROUP1, and GROUP1 there are 2 files of the form x_diff.nii where x is a digit between 2 and 7. At least that's how I read your code.
You don't in advance know which two digits there are.
The easiest way I see it is to run through all possible SubFolders like you do, but use the continue statement early to jump to the next if it doesn't exist:
Folders=(CONTROL GROUP1 GROUP2)
SubFolders=(2 3 4 5 6 7)
data_source=/Users/sheena/Desktop/test/
for j in ${Folders[#]}; do
cd ${data_source}/${j}/
for i in ${SubFolders[#]}; do
if [[ ! -e ${i}_diff.nii ]]; then
continue
fi
fslroi ${i}_diff.nii ${i}_nodif 0 1
done
done
You could replace the if clause above with a single line:
for i in ${SubFolders[#]}; do
[[ -e ${i}_diff.nii ]] || continue
fslroi ${i}_diff.nii ${i}_nodif 0 1
done
But I find the more expressive if - fi block easier to read and understand, and that's important too.
Related
Every time I write a new amount of data, two new directories are created called a sequence.
Directory 1 should always be 9 files larger than Directory 2.
I’m using ls | wc –l to output the number of files in each directory then manually doing the difference.
For example
Sequence 151
Directory 1 /raid2/xxx/xxxx/NHY274938WSP1151-OnlineSEHD-hyp (1911 files) – after WSP1 is the seq number.
Directory 2 - /raid/xxx/ProjectNumber/xxxx/seq0151 (1902 files)
Sequence 152
Directory 1 /raid2/xxx/xxxx/NHY274938WSP1152-OnlineSEHD-hyp (1525 files)
Directory 2 - /raid/xxx/ProjectNumber/xxxx/seq0152 (1516 files)
Is there a script that will output the difference (minus 9) for every sequence.
Ie
151 diff= 0
152 diff =0
That works great however:
I can now see some sequences in
Directory 1 (RAW/all files) it contains extra files that i dont want compared against diectory 2 these are:
At the beginning Warmup files (not set amount every sequence)
Duplicate files with an _
For example :
20329.uutt -warmup
20328.uutt -warmup
.
.
21530.uutt First good file after warmup
.
.
19822.uutt
19821.uutt
19820.uutt
19821_1.uutt
Directory 2 (reprocessed /missing files) doesn’t include warmup shots or Duplicate files with an _
For example :
Missing shots
*021386 – first available file (files are missing before).
*021385
.
.
*019822
*019821
*019820
Could we remove warmup files and any duplicates I should have number of missing files?
Or output
diff, D1#warmup files, D1#duplicate files, TOTdiff
to get D1#duplicate files maybe I could count the total number of occurances of _.uutt
to get D1#warmup files I have a log file where warmup shots have a "WARM" at the end of each line. in /raid2/xxx/xxxx/NHY274938WSP1151.log
i.e.
"01/27/21 15:33:51 :FLD211018WSP1004: SP:21597: SRC:2: Shots:1037: Manifold:2020:000 Vol:4000:828 Spread: 1.0:000 FF: nan:PtP: 0.000:000 WARM"
"01/27/21 15:34:04 :FLD211018WSP1004: SP:21596: SRC:4: Shots:1038: Manifold:2025:000 Vol:4000:000 Spread: 0.2:000 FF: nan:PtP: 0.000:000 WARM"
Is there a script that will output the difference (minus 9) for every sequence. Ie 151 diff= 0 152 diff =0
There it is:
#!/bin/bash
d1p=/raid2/xxx/xxxx/NHY274938WSP1 # Directory 1 prefix
d1s=-OnlineSEHD-hyp # Directory 1 suffix
d2=/raid/xxx/ProjectNumber/xxxx/seq0
for d in $d2*
do s=${d: -3} # extract sequence from Directory 2
echo $s diff=$(expr `ls $d1p$s$d1s|wc -l` - `ls $d|wc -l` - 9)
done
With filename expansion * we get all the directory names, and by removing the fixed part with the parameter expansion ${parameter:offset} we get the sequence.
For comparison here's a variant using arrays as suggested by tripleee:
#!/bin/bash
d1p=/raid2/xxx/xxxx/NHY274938WSP1 # Directory 1 prefix
d1s=-OnlineSEHD-hyp # Directory 1 suffix
d2=/raid/xxx/ProjectNumber/xxxx/seq0
shopt -s nullglob # make it work also for 0 files
for d in $d2*
do s=${d: -3} # extract sequence from Directory 2
f1=($d1p$s$d1s/*) # expand files from Directory 1
f2=($d/*) # expand files from Directory 2
echo $s diff=$((${#f1[#]} - ${#f2[#]} - 9))
done
I have an infinite loop which uses aws cli to get the microservice names, it's parameters like desired tasks,number of running task etc for an environment.
There are 100's of microservices running in an environment. I have a requirement to compare the value of aws ecs metric running task for a particular microservice in the current loop and with that of the previous loop.
Say name a microservice X has the metric running task 5. As it is an infinite loop, after some time, again the loop come for the microservice X. Now, let's assume the value of running task is 4. I want to compare the running task for currnet loop, which is 4 with the value of the running task for the previous run, which is 5.
If you are asking a generic question of how to keep a previous value around so it can be compared to the current value, just store it in a variable. You can use the following as a starting point:
#!/bin/bash
previousValue=0
while read v; do
echo "Previous value=${previousValue}; Current value=${v}"
previousValue=${v}
done
exit 0
If the above script is called testval.sh. And you have an input file called test.in with the following values:
2
1
4
6
3
0
5
Then running
./testval.sh <test.in
will generate the following output:
Previous value=0; Current value=2
Previous value=2; Current value=1
Previous value=1; Current value=4
Previous value=4; Current value=6
Previous value=6; Current value=3
Previous value=3; Current value=0
Previous value=0; Current value=5
If the skeleton script works for you, feel free to modify it for however you need to do comparisons.
Hope this helps.
I dont know how your input looks exactly, but something like this might be useful for you :
The script
#!/bin/bash
declare -A app_stats
while read app tasks
do
if [[ ${app_stats[$app]} -ne $tasks && ! -z ${app_stats[$app]} ]]
then
echo "Number of tasks for $app has changed from ${app_stats[$app]} to $tasks"
app_stats[$app]=$tasks
else
app_stats[$app]=$tasks
fi
done <<< "$( cat input.txt)"
The input
App1 2
App2 5
App3 6
App1 6
The output
Number of tasks for App1 has changed from 2 to 6
Regards!
Using wget im downloading 40k files with .deb extention,
After every 200 download of .deb files i want to create a new_directory and move those 200 files to that new_directory,
And so on for rest of the files
Please help me!!
An example on how to do something every nth time in a shell script:
for i in {1..20}; do
if [ $(($i % 2)) -eq 0 ]; then
echo $i
fi
done
This prints:
2
4
6
8
10
12
14
16
18
20
I expect you to download your files in a loop. If so, you may take the loop counter and modit by the desired amount (in your case 200).
When using a shell script file i want to be able to prewrite the input for the configuration.
how can i give the answers automatically?
there are multiple questions with short answers required for example- 'Please select an option: 1, 2 or 3' and provide answer 2. I considered using a heredoc but would this be the most appropriate option?
My example script:
./install.sh <<HERE
1
1
2
1
1 2 3
1 2
2
HERE
thanks for any help given.
I want to write a bin file to a flash drive. I'm supposed to run:
n helloworld.bin
l 0
w 0 0 0 1
But when I run l 0 I get a File not found error. What am I doing wrong?
Two issues:
MS-DOS filenames should have a maximum of 8 letters before the dot and a maximum of 3 letters after the dot.
For this use of the l command in debug, provide no parameters. The file will always be loaded to CS:0100.
(I somehow find it worrying that my brain saved this useless information for all those years...)