1

Am trying at create a bash script which would return the total space occupied by files inside macOS diectory ~/Pictures/Photos\ Library.photoslibrary/Masters/.

#!/bin/bash
# AUTHOR:SWASTI BHUSHAN DEB

    # Platfrom:macOS
    # macOS High Sierra 10.13.
    #Checks the total space occupied by files within a directory


    read -e -p "Enter the full path to the Home Directory : userpath
    var=$(sudo du -ach $userpath/Photos\ Library.photoslibrary/Masters/|tail -1|awk '{print $1}')
    echo "$var"
    exit

The command below when used solo in terminal provides the desired result:

`du -ach ~/Pictures/Photos\ Library.photoslibrary/Masters/|tail -1|awk '{print $1}'
25G

But when put in the script above with $userpath ,the script encounters error:

 du: /Volumes/Macintosh: No such file or directory
du: HD/Users/swastibhushandeb/Pictures/Photos Library.photoslibrary/Masters/: No such file or directory
0B

Am not sure whats wrong in this.Any comments/sugesstions/sample script code are welcome

  • This happens because the path to your user's home directory, /Volumes/Macintosh HD/Users/ contains a space. The solution to all such issues is to always double quote your variables as explained in the duplicate. – terdon Apr 02 '19 at 18:41

1 Answers1

1

You need to quote your variables when using them, otherwise paths with spaces and other special characters will not work:

read -e -p "Enter the full path to the Home Directory :" userpath
var=$(sudo du -ach "$userpath/Photos Library.photoslibrary/Masters/" |tail -1|awk '{print $1}')
echo "$var"
exit

PS: As you throw away most output from du you probably don't need the -a option.

nohillside
  • 3,251