2

I'm having a lot of trouble at work trying to save a long list of echo outputs as a .txt file on my desktop. I am using Bash in Yosemite 10.10.4. I am still very new to Bash so any help and tips are appreciated.

The goal is to print the name of the protocol used per brain scan, for a long list of brain scans. I used a for loop to recursively go through each brain scan, pull out the protocol used, then echo it and the path to the exact file used to acquire that information.

My script:

for i in /Path/to/scans/
do

for file in "$i/"001*/0001.dcm
do

# If there is no such file here, just skip this scan.
if [ ! -f "$file" ]
then
echo "Skipping $i, no 0001.dcm file here" >&2
continue
fi

# Otherwise take the protocol data from scan out
line= dcmdump +P 0040,0254 0001.dcm 
## dcmdump is the command line tool needed to pull out this data. 
## In my case I am saving to variable "line" the protocol used in 
## the scan that this single 0001.dcm file belongs to (scans require 
## many .dcm files but each one contains this kind of meta-data).

# Print the result
echo "$line $file"

break
done
done

So this script almost works. In my Terminal window, I do get a long list of protocols used, and the absolute filepath to the 0001.dcm file used for each scan.

My problem is, when I change it to

echo "$line $file" >> /Users/me/Desktop/scanparametersoutput.txt

The text file that appears on my desktop is blank. Anyone have any idea about what I am doing wrong?

6

One problem you're having with your script is in this line:

line= dcmdump +P 0040,0254 0001.dcm

Instead of assigning the output of dcmdump to line, it is running your dcmdump command with an environment variable called line set to ''. You can read more about this here.

So what you're actually seeing is the output of dcmdump being run by your script, not the output of $line, since $line isn't being assigned anything.

To capture the output of a program, use the syntax

line=$(dcmdump +P 0040,0254 0001.dcm)

(Note also that there is no space before or after the = sign to be safe.)

$() runs the code within the parentheses in a subshell and then 'replaces' itself with that the output of that code.

You probably want 0001.dcm within the dcmdump command to be $file instead as well, but I'm not familiar with it, so I'll leave that to you.

  • Thank you very much rmelcer! This solved my problem completely and gave me a nice output file. Thanks for explaining how/why/what I was doing wrong! So was it the space after the = that caused line to be set to ''? Tricky tricky! – Jordan Garner Aug 12 '15 at 23:30
3

As rmelcer mentioned, the line variable is not being set and it looks like the dcmdump is not running on the correct file:

line=$(dcmdump +P 0040,0254 "$file")

The setup and structure of the script look a bit more complex than they may need to be.

The outer path loop doesn't loop, but that might just be for your example?

Your file test is unlikely to fail, unless you have directories called 0001.dcm, as you specifically looking for 0001.dcm in the for loop.

for path in /Path/to/scans/; do
  for file in "$path/"001*/0001.dcm; do

    # If there is no such file here, just skip this scan.
    if [ ! -f "$file" ]; then
      echo "Skipping $file, no 0001.dcm file here" >&2
      continue
    fi

    # Otherwise take the protocol data from scan out
    line=$(dcmdump +P 0040,0254 "$file") 

    # Print the result
    echo "$line $file"

  done
done

If you can deal with the filename coming first in your output, this might be a bit simpler using find:

find /Path/to/scans/001* \
  -name 0001.dcm \
  -type f \
  -printf "%p " -exec dcmdump +P 0040,0254 {} + \
| tee /Users/me/Desktop/scanparametersoutput.txt

This looks in /Path/to/scans/001* for files (-type f) named 0001.dcm (-name 0001.dcm), prints out the full path to the file (-printf "%p ") and runs the dcmdump command on the file (-exec xxx {} +).

The output from the find command is then piped into tee which prints to screen and also to the specified file (/Users/me/Desktop/scanparametersoutput.txt)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.