Run Linux shell script on rust

Hi there,
I'm new to the Rust language and having a problem while coding. I want to create a wifi-hotspot on Linux. And when I'm running a shell script on the program to create it, I'm having an error.
Here I have mentioned how I tried to run the commands.

run_fun!(sudo iw phy phy0 interface add wlan0 type __ap);

When I run the above phrase, I'm having this as an error.

Error: Custom { kind: Other, error: "Running [\"sudo\", \"iw phy phy0 interface add wlan0 type __ap\"] exited with error; status code: 1" }

I have tried my best. I couldn't find the reason. Does anyone know how to achieve this?

The error message shows that the command argument array has only two elements: the first is sudo, and the second is the actual command shoved into a single giant string. That's not right – that second argument is treated by sudo as the command to be run, but it is not the command (i.e., the name of the executable you want to run is not iw phy phy0 etc., it's just iw, with the rest being arguments to it).

It looks like you are confused about what gets parsed by the shell – when you run a command directly, nothing goes through the shell. I have no idea what this macro is, but it's likely not correct. Consider using the built-in standard Command type for starting subprocesses.

2 Likes

Thanks to you I have learned a new thing in the Rust (Command). So, Thank you.

Actually, I need to run it with the sudo command. However, with your suggestion and this link, I was able to solve it.
This is how I used it. Seems to work fine.

let result = Command::new("sudo")
        .args(["iw", "phy", "phy0", "interface", "add", "wlan0", "type", "__ap"])
        .status()
        .expect("Error occurred...!");

If you have a better way to do this, I'm all eyes.

I was not saying that you shouldn't use sudo. I was saying that the (sub)command that is passed to sudo is not the long string itself; it's just iw, and the rest are its arguments.

1 Like

Ok. Thank you.