Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a script that is suppose to run a command in the background, and it does that. The problem is that when the script comes upon a read command, it doesn't pause and accept input. Here it is:

printf "Where is yo music?: "
read musicPath

cd $musicPath
ls | while read currentSong;do
  seconds=`mdls "$currentSong"|sed -n '20p'|awk '{print $3}'|cut -d. -f1`
  hours=$((seconds / 3600))
  seconds=$((seconds % 3600))
  minutes=$((seconds / 60))
  seconds=$((seconds % 60))
  echo "Song: $currentSong"
  echo "Length: $hours:$minutes:$seconds"
  afplay "$currentSong"&
  printf "yes (y), no (n), or maybe (m): "
  read choice
  case $choice in
    y)
      mkdir ../Yes
      mv "$currentSong" ../Yes
    ;;
    n)
      mkdir ../No
      mv "$currentSong" ../No
    ;;
    m)
      mkdir ../Maybe
      mv "$currentSong" ../
    ;;
    *)
      echo "Invalid option! Continuing..."
    ;;
  esac
  kill $!
done
share|improve this question
    
in bash, you can provide the prompt in the read command itself: read -p "where is yo music? " musicPath –  glenn jackman Aug 12 '12 at 23:51

1 Answer 1

up vote 14 down vote accepted

There are numerous issues with that script, but the one thats causing your specific issue is because you're reading from a pipe (the output of ls).

1. Don't parse ls

Use this instead

for currentSong in *; do
  ...
done

Aside from the numerous reasons you shouldnt parse ls, the issue you're seeing is because STDIN is connected to the output of ls. So when you issue a read, it can't read from the terminal because STDIN isn't connected to the terminal.


2. Use more quotes

You've got a fair amount of quotes spread around, but still missing some. Mainly just on the cd.

cd "$musicPath"

also

case "$choice"


3. Don't use backticks

Using backticks is ok sometimes. I frequently use them on the command line as it faster to type than $(). But for scripting, it's good practice to use $() instead.

seconds="$(mdls "$currentSong"|sed -n '20p'|awk '{print $3}'|cut -d. -f1)"


4. mkdir

Your mkdir will generate an (harmless but noisy) error if the directories already exist. Add a -p in there which will cause the mkdir to silently do nothing if it already exists

mkdir -p ../Yes


Yes, there are a lot of pitfalls with bash. Not trying to be harsh, just trying to break bad habits.
Have fun :-)

share|improve this answer
    
Thanks for all of the tips! I love this stuff, so no worries. Always love learning new things (: –  Cade Aug 12 '12 at 4:42
    
+1 Very helpful advice, thanks –  Anthony Aug 13 '12 at 11:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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