Problem setting path to external hard drive as a variable

Hello all,

I am EXTREMELY new to using bash and I have a bit of a problem: I'm trying to set up a shell script that can
  • 1.) take one of several giant files off of an external hard drive
  • 2.) use it as input for scripts on my laptop's hard drive
  • 3.) send output of those scripts to the external hard drive
  • 4.) delete the giant file off my laptops hard drive
  • 5.) repeat

Everything is set up in a cript i wrote that performs this process using three different folders on my laptop, but when i go to pull files off my external hard drive I find I am unable to do so! I've poked around and tried to find the problem, but as a true novice I'm having no luck. Heres what it looks like:

#!/bin/sh
prev_dir= /Volumes/My\ Passport/test_reads_folder_a
new_dir=/Users/mylapple/desktop/test_reads_folder_b

cd $prev_dir
for i in `cat targeted_files.txt` 
do
   sed -i '' 's/\r$//' $i 
   echo $i
   cd $prev_dir          
   cp $i $new_dir         
   
done

Gives me the output:

./4fqmv.sh: line 11: cd:  /Volumes/My Passport/test_reads_folder_a: No such file or directory
cat: targeted_files.txt: No such file or directory

Any suggestions?

1 Like

Hi

prev_dir= /Volumes/My\ Passport/test_reads_folder_a

remove the space after the equal sign

prev_dir=/Volumes/My\ Passport/test_reads_folder_a

Enclose the variable in double quotes.

cd "$prev_dir"

--- Post updated at 05:49 ---

instead of this command. so do not override variables

sed -i '' 's/\r$//' $i
echo $i

use this expression.

echo ${i%?}

--- Post updated at 06:20 ---

It's all unnecessary

#!/bin/sh
prev_dir=/Volumes/My\ Passport/test_reads_folder_a
new_dir=/Users/mylapple/desktop/test_reads_folder_b
file=targeted_files.txt
sed 's/\r$//' "$prev_dir"/$file >"$new_dir"/$file

--- Post updated at 06:31 ---

If this experiment then is better so

while read -r d; do
    sed 's/\r$//' <<<"$d"
done <"$prev_dir"/$file >"$new_dir"/$file

--- Post updated at 06:40 ---

I tried to delete a character \r so
echo ${var%\r} and so echo ${var%[\r]} Did not work out.
This expression is not correct since any last character will be deleted. echo ${var%?}
The only way echo ${var%[[:cntrl:]]}

1 Like

With bash , you have the "special treatment" of $'\xxx' strings during "parameter expansion". So nezabudka's proposal could be written like

echo ${var%$'\r'}

But I'm not sure that would remove your error message. Sure that directory exists? Doesn't have non-printing characters in its name? Is the external drive correctly mounted, and where?

Your entire script could be somewhat condensed:

#!/bin/bash                                             # have it run by bash, NOT sh, so bash new functionality is enabled
prev_dir=/Volumes/My\ Passport/test_reads_folder_a
new_dir=/Users/mylapple/desktop/test_reads_folder_b

for FN in $(sed 's/\r$//' targeted_files.txt)           # remove <CR> in the first place, running sed once in lieu of umpteen times
  do    cp $prev_dir/$FN $new_dir                       # use entire paths for the copy action
  done
1 Like

Thanks nezabudka!

I plugged in your first suggestion and it seems to work just fine! I think I will use that code for now, though I will study your's and other commenters' more compact, elegant solutions later when I have more time: I'm such a neophyte I actually find that the least elegant, most sprawling code is often the easiest for me to understand. Its like learning how clocks work from disassembling one: you want to start with a cheap, simple bedside alarm clock, not a finely crafted rolex!

Again, thank you so much for your help!