Reputation: 15
Below is a little script I need to get working, but not sure how as I have never came across this before and unable to figure it out. I need to get the chmod commands embedded into the startlogin.sh file. For the commands to work in the startlogin.sh file I need to have the single quotes like below in the chmod commands. But sed doesn't like them in there and will error when they are there. I need to have the single quotes there, but still push out the commands using sed. Anyone know what the workaround would be for this? This is sed on Mac OS 10.9.5.
sed -i '' '/lpoptions/ a\
\
chmod +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/htdocs \
chmod -R +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/logs \
chmod -R +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/tmp \
chmod -R +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/db \
' /private/var/root/Library/Scripts/startlogin.sh
Upvotes: 0
Views: 81
Reputation: 189937
Unless you have extrinsic reasons to require single quotes (like, say, you need to interact with another tool which forces this choice), the simple and elegant solution is to use double quotes in the chmod +a
arguments. In your example, the argument strings contain no shell metacharacters, so you can use any quotes you like.
sed -i '' '/lpoptions/ a\
\
chmod +a "everyone allow read,list,search,add_file,add_subdirectory,delete_child" /Applications/MAMP/htdocs \
chmod -R +a "everyone allow read,list,search,add_file,add_subdirectory,delete_child" /Applications/MAMP/logs \
chmod -R +a "everyone allow read,list,search,add_file,add_subdirectory,delete_child" /Applications/MAMP/tmp \
chmod -R +a "everyone allow read,list,search,add_file,add_subdirectory,delete_child" /Applications/MAMP/db \
' /private/var/root/Library/Scripts/startlogin.sh
You could additionally simplify the script by observing that chmod -R +a stuff x; chmod -R +a stuff y
can be refactored to chmod -R +a stuff x y
(as long as you have the same stuff
in both).
Upvotes: 1
Reputation: 782775
One solution is to escape all the quotes, as in Etan Reisner's answer. Another way is to put your sed
commands in a file, and refer to the file with the -f
option. So create a file called lpoptions.sed
, and put:
/lpoptions/ a\
\
chmod +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/htdocs \
chmod -R +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/logs \
chmod -R +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/tmp \
chmod -R +a 'everyone allow read,list,search,add_file,add_subdirectory,delete_child' /Applications/MAMP/db \
\
in the file. Then do:
sed -i '' -f lpoptions.sed /private/var/root/Library/Scripts/startlogin.sh
Upvotes: 2
Reputation: 81052
You cannot embed single-quotes in a single-quoted string. That's not a sed issue that's a shell issue. sed isn't seeing the internal single-quotes at all in your example. The shell is eating them.
To "escape" a single-quote inside a single-quoted string you have to use this ugly thing: '\''
for each "internal" single-quote.
Upvotes: 2