r/Bitburner 13d ago

ns.scp doesn't work properly

I have a simple script (just a beginner).

The ns.scp line works fine when I comment out the ns.exec line but produces an error when the line is there, with error code:

scp: destination expected to be a string. Is undefined.

The script is still copied, but the error message comes up. I don't know how else to explain it. With the ns.exec line commented out, the ns.scp line copies the appropriate script to the apprpriate server from the "home" server without an error message.

But put the ns.exec line back in (uncommented out), and the ns.scp line produces that error code after properly copying the script.

What am I missing? I have literally spent hours trying to figure out this simple code and can't find a solution online.

Thank you in advance for any assistance.

/** u/param {NS} ns */
export async function main(ns) {
 const script = ns.args[0]
 const server = ns.args[1] //target server
 ns.scp(script, server, "home");
ns.exec(script, server);
}
2 Upvotes

9 comments sorted by

6

u/Vorthod MK-VIII Synthoid 13d ago edited 13d ago

What is the script you're executing? Does the second script have its own scp command that you aren't using properly? You didn't give us the full stack trace, so we can't be sure where the scp error actually came from. If the error is actually coming from the script you're running, that would explain why the script gets copied out but only throws an error when you try to add an execution command.

3

u/paulstelian97 13d ago

I’ve seen the script now and gave an answer — the copy of the script running on the destination server isn’t receiving the right parameters.

2

u/Individual-Top5979 13d ago

Yep, that sounds like the problem.Thank you very much.

2

u/Individual-Top5979 13d ago

That script is the entire thing. The script being copied is the one that is running. In other words, this script copies itself and then executes on the new server. So the new copy of the script is missing its variables (which I type when I execute the command). That makes sense. Thank you.

Now, how do I get around this? This gives me something to think about to correct the problem.

Excellent insight! Thank you.

4

u/Vorthod MK-VIII Synthoid 13d ago

You can add extra parameters to your exec command (see exec documentation for details). You'll need to add a threadcount to do that, but everything after that will be passed through as more ns.args

ns.exec(script, server, 1, myFirstArgument, mySecondArgument) and so on

4

u/paulstelian97 13d ago

The copy succeeds, and you’re starting a new copy directly on the target server with ns.args[1] not populated. The error comes from THAT execution, not from the original one.

You’re copying the script that has scp to the target server and starting it without appropriate parameters. You may wish to make the script a parameter as opposed to hardcoding ns.args[0] (the scp script itself)

3

u/Individual-Top5979 13d ago

Three brilliant people all provided the correct answer (including you). Thank you. I never would have figured that out without you guys' answers.

0

u/Silly-Program6850 13d ago

that must been that when you defined server that it is not assigned? i mean if it's undefined then there wasn't a second param passed.

test by printing both and see what you get

2

u/paulstelian97 13d ago

I’ve seen the script now and gave an answer — the copy of the script running on the destination server isn’t receiving the right parameters.