0

super new to bash

I wrote a bash script that works perfectly on one MAC but doesn't on a different MAC.

The code iteratively cycles through subjects' folders, copies the data, performs some function, deletes the copied data, then moves to the next subject. To do so, I call a text file that contains the subject names, which informs the script to select the corresponding subject folder (using the variable $mri_path in the code below).

The problem is, on this new MAC- the script can't find the subject's folder using a text file- error code: file/directory does not exist. A main difference between MACs is the text file being used- it contains different subject names. I can hardcode the script to find the subjects' folders, so this leads me to suspect the text file is not being read correctly.

I have tried different text file formats (e.g., unicode, Macintosh formatted, tab delimited)... so I don't know why the script can't find the subject folders.

Reduced code:

#!/bin/bash 

#usage: ./dcm2nii_batch.sh subj.txt startline endline


s1=$(($2-1))
s2=$(($3-1))

vars=($(awk -F= '{print$1}' subj.txt));

for ((i=s1; i<s2; i++));
do mri_path=${vars[$i])};

echo $mri_path;
echo '***';
cd /Users/syschaef/Desktop/dcm2nii_batch/Raw/$mri_path

The error occurs at the last line as the cd command fails.


The text file data are subject identifying numbers that correspond to subject folder names/directories (screenshot):

enter image description here

I appreciate you taking the time to read and/or respond!

jack
  • 106
  • 9
  • Side note: the entire last loop can be replaced by `rm */*.dcm` – Benjamin W. Dec 15 '21 at 16:06
  • Can you reduce the script to just keep the part around that causes the error? I dont' understand where the errors exactly happens, or what "find the subject's folder using a text file" means. – Benjamin W. Dec 15 '21 at 16:08
  • @BenjaminW. thanks for responding! 1) I reduced the code- it now ends at the 'change directory' command that fails. 2) "find the subject's folder using a text file" means the script calls a text file that contains subject names, which informs the script to select a specific folder that corresponds to the subject. E.g., the script will call the first line of the text file 'subject 1' and then open the folder called 'subject 1'; the next iteration of the loop will call the next line of the text file 'subject 2' and then open the folder called 'subject 2' – jack Dec 15 '21 at 16:22
  • What are the contents of the text file? Are there paths with spaces in them? Try adding enough information such that anybody can reproduce the problem with just what's in the question ([mcve]). – Benjamin W. Dec 15 '21 at 16:29
  • Use https://www.shellcheck.net/ – Diego Torres Milano Dec 15 '21 at 16:45
  • @BenjaminW. the contents of the text file are subject-identifying numbers that correspond each subject's folder/directory (I attached a screenshot of the contents of the text file). – jack Dec 15 '21 at 16:51
  • Post a copy-pastable excerpt instead of a screenshot so people can try and reproduce locally. Notice that your script snippet is now invalid as the `for` loop is missing its `done`. – Benjamin W. Dec 15 '21 at 17:26
  • 1
    I suspect there might be Windows line endings in your text file (with carriage return, `\r\n` instead of just `\n`). Try `dos2unix` on the file before running your script. Also, to read a file line by line, have a look at [BashFAQ/001](https://mywiki.wooledge.org/BashFAQ/001) and [Looping through the content of a file in Bash](https://stackoverflow.com/q/1521462/3266847) – Benjamin W. Dec 15 '21 at 17:30
  • @BenjaminW. `dos2unix` fixed the text file (so basic)- thanks! – jack Dec 15 '21 at 19:25

1 Answers1

1

The script contains syntax errors that's why I recommended https://www.shellcheck.net/.

The subj.txt may contain chars not visible in your screenshot. Try xxd subj.txt and compare with the one that works.

You can also fix the file using

dos2unix subj.txt

in case there are Windows line ends.

Before doing something check for potential problems

d="/Users/syschaef/Desktop/dcm2nii_batch/Raw/$mri_path"
if [[ ! -d "$d" ]]
then
    printf 'ERROR: %d not a directory\n' "%d" >&2
    exit 1
fi
cd "$d"
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • thanks Diego! I've never looked at a hex dump before. there's a difference between the two files. the text file that works has [what I think] is a delimiter of one decimal pt and the file that doesn't work has two decimal points between subject names. I manually entered the subject names in the file that works, but for the one that doesn't work, I used this script because there were too many subjects to manually enter in the text document: for d in ./*;do [[ -d "$d" ]] && echo "$d" >> subj.txt; done – jack Dec 15 '21 at 18:10
  • @jack `xxd` displays nonprinting characters as "`.`"; you should look at the corresponding hex listing to see what the characters actually are. My guess is that the nonworking file has DOS/Windows-style line endings, which have a carriage return character (hex `0d`) in addition to the linefeed (`0a`) that unix expects. See ["Are shell scripts sensitive to encoding and line endings?"](https://stackoverflow.com/questions/39527571/are-shell-scripts-sensitive-to-encoding-and-line-endings) – Gordon Davisson Dec 15 '21 at 18:41
  • `dos2unix` worked- thanks for the solution (and detailed explanation @GordonDavisson) – jack Dec 15 '21 at 19:27