I used following piece of code to list down the contents of a directory in macOS:
#!/bin/bash
echo "hello User"
echo ""
userdir=$1
var_one=$userdir\/Library\/Application\ Support\/iCloud\/Accounts\/
if [ -d "$var_one" ]
then
# echo "Direcotry exists"
echo "The AppleID is : "
sed -n '/[*\w-]+@([\w-]+\.)+[\w-]+/p'|ls "$var_one"|awk NR==2'{print}'
echo "~~~~~~~BYE~~~~~~"
break
else
echo "Directory doesn't exists"
echo "The path is $var_one"
fi
If the directory exists then sed command is used along with regex pattern to identify the desired file. The script works fine but doesn't seem to exit.
STDIN– Archemar Mar 27 '19 at 05:56- You dont need to escape the
- break is only valid in loops.
– Mike V.D.C. Mar 27 '19 at 06:36/. You just need to escape the spaces.sedis unlikely to know what\wis (macOSseddoes not know PCRE expressions). Instead, use[[:alnum:]_](this is a POSIX character class equivalent to\w). – Kusalananda Mar 27 '19 at 06:50