basename extra operand in GitHub actions - bash

I have a shell script that is supposed to dynamically bundle and zip binaries based on the directory names in cmd/
When using the script locally, it works like a charm. However when the script is ran in github actions, I get the error
basename: extra operand ‘./cmd/get-reminders/’
I have a function that collects all dir names and pushes them to an array. The error above is happening when this function is ran.
prepare() {
for d in "./cmd/*/"
do
handlers+=($(basename $d))
done
}
Any ideas what the error actually is, and why github actions throws this error, and why its not happening locally?
I'm running zsh locally, github uses. bash. I don't think that's the problem, but I'm mentioning it anyway.
Thanks.

Seems like the issue was looping over a path inside quotes.
for d in "./cmd" does not work in some shell.
Correct would be for d in ./cmd

Related

Rust std::process with special characters

I've got a very simple rust program but its not doing quite what I'd expect. Running on Windows, using a powershell prompt, I can do the following to display the path:
echo "%PATH%"
and I have a simple Rust program with:
Command::new("echo")
.arg("%PATH%")
.spawn()
.expect("ls command failed to start");
The command will launch and run, but it outputs:
%PATH%
instead of the path contents, like I'd expect. Other commands which don't use special characters seem to work as expected, so I suspect its related to handling them but I don't see a way in Rust to make the command any more primitive than it already is.
I've tried various formatting but it either fails to run the command or does the same.
I also tried using $env:path, but this always fails to run from Rust with a cannot find the path specified error.
Are there any suggestions for handling this? I could write the contents to a file and run the file instead, but running these types of commands from other languages directly works fine so I think it should work from Rust as well.
Thanks!
Update:
Managed to get the expected results from by using:
let out = Command::new("cmd")
.arg("/C")
.arg("echo %PATH%")
.spawn()
.expect("ls command failed to start");
}
I think the question got interpreted a bit differently, as getting the path was just an example of a larger issue I was seeing. Updating with the above solved my larger issue as well.
As the comment by French says: Spawning the process does not include the Powershell-environment, which would expand %PATH% to it's actual content before launching the process.
You need to get the content of PATH via std::env yourself or lookup the Powershell documentation on how to launch a subprocess inside a powershell-session.
As others have mentioned, it's not the special characters, it's the fact that those special characters are interpreted by powershell before the "echo" program runs at all.
Using https://doc.rust-lang.org/cargo/reference/environment-variables.html as a reference for how to look up environment variables, try something like this:
use std::env;
fn main() {
let cur_path = env::var("PATH").unwrap();
println!("Environment is: {}", cur_path);
}
You can try this here: https://play.rust-lang.org/
You can then feed cur_path into your "Command::new" if you wish. The trick is that powershell substitutes that argument BEFORE launching echo, which you may not have known, whereas when you execute the echo program directly, you have to do that substitution yourself.

Creating VM with Azure CLI

I'm posting to see if someone can point me in the right direction. I've been trying to get this working for a few days now and I'm at a dead end.
I have tried to remove " around the variables and remove variables completely. I have tried to run this in Azure Cloud Shell, WSL, and PowerShell as a .ps1 script. However, I continue to get the same type of errors.
Here is the script.
Here is the error I am getting.
validation error: Parameter 'resource_group_name' must conform to the
following pattern: '^[-\w\._\(\)]+$'. ' not recognized. ' not found. Check the spelling and casing and
try again.
If I run a one liner with out variables I get this error.
az vm create: error: local variable 'images' referenced before
assignment
Any help would be greatly appreciated. Thank you!
So for the first error, looks like the create VM is initiating to fast to recognize the new resource group.
For the image you need to use the full URN MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest
First, it's a bash script and the variable definition is different between Windows and Linux. So I suggest you run the script in the Azure Cloud Shell with bash mode or the WSL when it's a bash script.
Second, the error shows the resource group name does not meet the Naming rules and restrictions for Azure resources, you need to read the document to check if the group name is right.
So I have found out that the issue was. It was how VS Code formats bash scripts. I'm not exactly sure how VS Code is formatting but I think it might have something to do with ASCII. I recreated the script in Azure CLI with nano and it runs fine as a bash shell script. Thanks everyone for your help.

bash commands to remote hosts - errors with writing local output files

I'm trying to run several sets of commands in parallel on a few remote hosts.
I've created a script that constructs these commands, and then writes the output in a local file, something along the lines of:
ssh <me>#<ip1> "command" 2> ./path/to/file/newFile1.txt & ssh <me>#<ip2>
"command" 2> ./path/to/file/newFile2.txt & ssh <me>#<ip2> "command" 2>
./path/to/file/newFile3.txt; ...(same repeats itself, with new commands and new
file names)...
My issue is that, when my script runs these commands, I am getting the following errors:
bash: ./path/to/file/newFile1.txt: No such file or directory
bash: ./path/to/file/newFile2.txt: No such file or directory
bash: ./path/to/file/newFile3.txt: No such file or directory
...
These files do NOT exist but will be written. That being said, the directory paths are valid.
The strange thing is that, if I copy and paste the whole big command, then it works without any issue. I'd rather have it automated tho ;).
Any ideas?
Edit - more information:
My filesystem is the following:
- home
- User
- Desktop
- Servers
- Outputs
- ...
I am running the bash script from home/User/Desktop/Servers.
The script creates the commands that need to be run on the remote servers. First thing first, the script creates the directories where the files will be stored.
outputFolder="./Outputs"
...
mkdir -p ${outputFolder}/f{fileNumb}
...
The script then continues to create the commands that will be called on remotes hosts, and their respective outputs will be placed in the created directories.
The directories are there. Running the commands gives me the errors, however printing and then copying the commands into the same location works for some reason. I have also tried to give the full path to directory, still same issue.
Hope I've been a bit clearer.
If this is the exact error message you get:
bash: ./path/to/file/newFile1.txt: No such file or directory
Then you'll note that there's an extra space between the colon and the dot, so it's actually trying to open a file called " ./path/to/file/newFile1.txt" (without the quotes).
However, to accomplish that, you'd need to use quotes around the filename in the redirection, as in
something ... 2> " ./path/to/file/newFile1.txt"
Or the first character would have to something else than a regular space. A non-breaking space perhaps, possible something that some editor might create if you hit alt-space or such.
I don't believe you've shown enough to correctly answer the question.
This doesn't look like a problem with ssh, but the way you are calling the (ssh) commands.
You say that you are writing the commands into a file... presumably you are then running that file as a script. Could you show the code you use to do that. I believe that's your problem.
I suspect you have made a false assumption about the way the working directory changes when you run a script. It doesn't. You are listing relative paths, so its important to know what they are relative to. That is the most likely reason for it working when you copy and paste it... You are executing from a different working directory.
I am new to bash scripting and was building my script based on another one I had seen. I was "running" the command by simply calling the variable where the command was stored:
$cmd
Solved by using:
eval $cmd
instead. My bad, should have given the full script from the start.

Shell script error in UNIX but works in CentOS - source: not found

myconf.sh
setting1=val1
setting2=val2
export setting1
export setting2
Then I call this conf file in my runner.sh, but I get the error "runner.sh: source: not found". Initially I was using source myconf.sh when calling it inside and saw in some posts that I should be using "." instead of "source".
myrunner.sh
#!/bin/sh
. myconf.sh
echo "$setting1"
echo "$setting2"
I'm calling myrunner.sh through this command
sh myrunner.sh
Please let me know if I'm actually doing something wrong. I'm able to call it properly without errors in my CentOS image but when we execute it in a UNIX box I'm hitting the error.
Thanks!
What is your $PATH env var set to?
You may just need to include the current directory in your path or prefix the filename.
Example: source ./myconf.sh
ETA: As noted; putting the current directory . into your path may not be the most desirable thing. I suggested it simply to explain why your script may work on one system but not another.

Function executing a Bash script not working ("No such file or directory")

So, I wrote a function to execute a Bash script just to circumvent taking my sweet time to navigate to the directory of that script.
However, when I launch the function (via an alias), Bash tells me there's no such file or directory! It's nuts!
Here's the function in question:
function torBrowser()
{
echo "Running Tor browser..."
bash "/home/user/Downloads/tor-browser_en-US/start-tor-browser.sh"
}
The access modes for the script and the parent directories are all rwxr-xr-x, so, technically, it should work with a couple of tweaks here and there somewhere.
Nothing obviously wrong with that. I recreated it here and it worked for me. It's got to be a typo or something. If you do $ls /home/user/Downloads/tor-browser_en-US/start-tor-browser.sh do you get an error? You are replacing "user" with your actually user name, right? :)

Resources