0

I'm working on a tool for a client which needs to be run as sudo (I found out later that it doesn't work without sudo as I've never worked on a Mac before). I have the binary ready, and I wrote a shell script to call that binary as sudo.

However, the problem is that the shell script isn't able to find the binary, and also it outputs the wrong path with pwd. The location of the binary isn't fixed and it could be placed anywhere, so I cannot hardcode the path into the shell script.

To elaborate a little on the pwd outputting wrong path: the script was placed in a location whose path is /Volumes/MySSD/MiscFiles/ and the output of pwd through the shell script was /User/<my client's username>.

Another important constraint is that my client cannot operate a terminal, so for him when he double clicks on the executable it starts. But I need this to run as sudo otherwise it doesn't work.

So, my question is, is there a way to make it so that when I double click the binary from the file explorer (I think it is called Finder in Mac), it should run as sudo.

Or,is there something wrong I'm doing in my shell script?

this is my shell script

#!/bin/bash
# sudo ./actiontool #actiontool is the binary, and this command didn't work, it gave a no such file or directory error
# echo $(pwd) #this echoed the wrong path
sudo .$(pwd)/actiontool #also didnt work as $(pwd) outputs the wrong path
16
  • 1
    You have too many variables here. Why do you need a shell script. If the executable couild be anywhere how does the user know how to call it? I suspect the end result is that you need to make it an app. ALso what does it need sudo for - macOS is a Unix so should behave as other Unices
    – mmmmmm
    Jun 18 at 10:59
  • I initially wasn't using a shell script. The tool transfers files over http, and it was giving permission errors without sudo. So I wrote a shell script so that my client can simply double click on the shell script from Finder and then it'll open the terminal, ask for the password and the script will work normally. All this because my client cannot operate a terminal, no other reason
    – RishiC
    Jun 18 at 11:12
  • @mmmmmm MacOS is quite restrictive with what you can do as a non-root user. Jun 18 at 11:13
  • 2
    This should not require sudo access. I think you have an X-Y problem here. Consider opening a new question asking how to do this - it might turn out to be very simple. Jun 18 at 11:30
  • 3
    MacOS has rsync. You really need a good reason for reinventing another wheel. Jun 18 at 14:03
1

The full path of the shell script including its name is in $0. You can extract the path by using ${0%/*}.

This doesn't solve other issues though:

  • Double-clicking the script will open Terminal which might confuse the user
  • sudo will only work as expected if the user is an Administrator (or if you changed /etc/sudoers but I assume you didn't)
  • They still need to enter their password in the Terminal window.

PS: The probably better way to approach this might be handling any access right issues from within the application.

1
  • Ok, I will try this. Automatically opening the terminal isn't a problem as he only needs to enter his password, which I showed him and he is quite comfortable with it
    – RishiC
    Jun 18 at 11:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .