Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Mac/Unix SHELL script help

Hello,

I would really appreciate it if a seasoned Unix Shell script user could look over my code and explain how I can fix it to make it work. I got this script from a coworker and I don't know how to use it.

Here is the script:

#!/bin/sh
filename="$1"
output="${filename%.*}_modified.csv"
cd $(dirname $filename)
string1="Reading #2 from /home/njhunsak/images/"
string2="/antsCorticalThickness/BrainSegmentation.nii.gz"
grep -vwE "(Reading #1|LabelID|^(    0))" ${filename} | sed -e "s|$string1||" -e "s|$string2||" -e "s|/|,|" -Ee "s/[ ]+/,/g" > temp.txt
awk '/^[0-9][0-9][0-9],/{for(i=0;i<6;i++)print}' temp.txt > part1.txt
awk '/^,/{print}' temp.txt > part2.txt
sed -i '' -e "s|,1,|,csf,|" -e "s|,2,|,gm,|" -e "s|,3,|,wm,|" -e "s|,4,|,subcortical,|" -e "s|,5,|,brainstem,|" -e "s|,6,|,cerebellum,|" part2.txt
paste part1.txt part2.txt > ${output}
perl -pi -e 'print "cabilid, subjectid, label, mean, std, max, min, count, volume (mm^3), extent x, extent y, extent z\n" if($.==1)' ${output}
rm part1.txt
rm part2.txt
rm temp.txt

What I have tried to do is open terminal and type "sh ~/Desktop/script.sh" but after I press enter to send the command, the next line says "usage: dirname path" and I don't know how to proceed.

I tried typing in the directory path that my data.txt is in, but pressing enter after typing that doesn't do anything.

I am glad I can actually run the script but it seems I don't know what to do once I am in the script.

Was I supposed to change some text in the script or is this script interactive and asking me to press a special button to proceed?

Thanks in advance for helping this newbie!

Add a code block to it so we can read it. Since bash doesn't require ; to end a line can't tell what's doing what. add ``` at the start and end of your code with a blank line before and after it.

It is really the first three lines of the code that give me trouble. Making this script find the data.txt file it needs is my main problem (I dont understand the first 4 lines and everything after makes sense as it moves text around and renames stuff)

2 Answers

So you would do "script.sh /home/user/data.txt". Of course use the correct directory.

1000 thanks my friend! Thanks for explaining how arguments work. I appreciate the help!

It's needing a filename as a run argument. So you have to run it from terminal command line.

So, how do I make the file "data.txt" be the first argument? Where do I add that to the code, or where do I do that in command line?