0

Using MacOSX Terminal

I am trying to make a bash script that will put screenshots into a newly made directory for the day. I am able to create the directory, as well as a .txt file with the screenshot names and file extensions. I create a variable to hold the file. For some reason doing this causes cat to return the "No such file.." error.

Here is essentially my code:

archive_screenshots(){

ymdDate=$(date +%Y-%m-%d)
#[stuff to make directory, which works]

scFile="~/Desktop/sc.txt"
scDir="~/Desktop/Screenshots/$ymdDate"

#screenshots have current date in them so grep to find

ls ~/Desktop/ | grep $ymdDate | sed 's/ /\\ /g' > "$scFile"

#File gets created, and is visible in Finder
cat "$scFile"

}

The error shows as "cat: ~/Desktop/sc.txt: No such file or directory When I run "cat ~/Desktop/sc.txt" in the Terminal it displayed the file properly

Ryan
  • 1
  • 1
    I think the `~` is not getting expanded. Use `$HOME` instead. – jordanm Aug 18 '20 at 02:11
  • @jordanm you're a wizard, this resolved my issue and after a bit of tweaking i got the whole script working. Thank you! – Ryan Aug 18 '20 at 02:51

1 Answers1

0

From @jordanm's response:

My bash was not expanding the '~' shortcut, and using ${HOME} in it's place resolved the issue.

scFile="${HOME}/Desktop/sc.txt"

cat "$scFile"
Ryan
  • 1