Run multiple commands using wsl - windows-subsystem-for-linux

I have some scripts on my home folder that I would like to run from command line, actually from the task scheduler, but I don't find the way to do so.
I have tried
wsl -u Ubuntu -u jlanza "cd /home/jlanza/bin && ./myscript && ./myotherscript"
but it doesn't work.
How can I concatenate the execution of several commands under the same session?

You need to escape && so powershell does not see them as different commands, to pass && to wsl do the following:
wsl -d Ubuntu -u jlanza -- cd /home/jlanza/bin `&`& ./myscript `&`& ./myotherscript

You need to pass those commands to WSL without the quotes so that bash interprets them correctly as a series of commands chained together, rather than a single long command. Unfortunately, when you do that, the '&&' operator gets interpreted by the Windows command line interpreter and the commands following do not get passed to WSL.
What I've found to work is replacing the '&&' (run command only if the preceeding command exits with success) operator with simple ';' (run command regardless of how the preceeding command exited). In your case then, something like this should work:
cmd /c "wsl -u Ubuntu -u jlanza cd /home/jlanza/bin; ./myscript; ./myotherscript"
However, if your use cases neccesitates the use of the '&&' operator, I'd try saving the command you're running as a script or an shell alias in the WSL. Calling that would then save you the need of passing '&&' through the Windows command line interpreter.

C:\Windows\System32\wsl.exe -d Ubuntu -u jlanza sh -c ". ~/.profile && script-jlanza.sh"
This way I load my own own profile and then I'm able to run the commands.
The option is just to run sh or any shell and then execute the commands you'd like within that shell. The other answer is also valid, but I like mine most, as I'm able to use the user profile (path, aliases, etc.)

Related

Why is $PATH different when executing commands via SSH and libssh?

I'm trying to run a command on a remote host via libssh2 as wrapped by the ssh2 Rust crate.
So I would like to run the command cargo build, but when I try to run it via libssh, I get the error:
cargo: command not found
However, when I ssh into the server manually from the command line everything works fine.
I have noticed that the $PATH is different when running ssh from the command line and libssh as well:
for instance when I echo $PATH
ssh gives me:
/home/<user>/.cargo/bin:/usr/share/swift/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bi
while libssh gives me:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
So it looks like what's happening is that the modifications made to $PATH inside .bashrc and .profile are not making it in when running via libssh.
I also get the same behavior if I run /bin/bash -c "echo ${PATH}"
Why would this be the case, and is there any way to get the same behavior in both these cases?
Please take a look at that question.
TL;DR A login shell first reads /etc/profile and then ~/.bash_profile. A non-login shell reads from /etc/bash.bashrc and then ~/.bashrc.

How to make shell script run by double-click?

I have a script which is executable from command line but I want to make it user friendly as I want to run this script by double clicking on it. How it is possible?
#! /bin/bash
cd
cd Desktop/D_usman/
java -jar imageSynch.jar
You may need to add ".command" to the end of the name of your shellscript on Mac OS X.
You need to add execute permissions for the user, group, or rest of the world, depending on who should be allowed to execute it. Look into chmod for more information.
Example: chmod u+x myscript
Once you do this, you can also start the shell script like this ./myscript instead of sh myscript.sh
Note: You can also make your JAR start by adding execute permission, given Java is setup correctly on your machine.

Unable to run a postgresql script from bash

I am learning the shell language. I have creating a shell script whose function is to login into the DB and run a .sql file. Following are the contents of the script -
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
echo "Running SQL Dump - auto_qa_db_sync"
\\i auto_qa_db_sync.sql
After running the above script, I get the following error
./autoqa_script.sh: 39: ./autoqa_script.sh: /i: not found
Following one article, I tried reversing the slash but it didn't worked.
I don't understand why this is happening. Because when I try manually running the sql file, it works properly. Can anyone help?
#!/bin/bash
set -x
echo "Login to postgres user for autoqa_rpt_production and run script"
$DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT -f auto_qa_db_sync.sql
The lines you put in a shell script are (moreless, let's say so for now) equivalent to what you would put right to the Bash prompt (the one ending with '$' or '#' if you're a root). When you execute a script (a list of commands), one command will be run after the previous terminates.
What you wanted to do is to run the client and issue a "\i ./autoqa_script.sh" comand in it.
What you did was to run the client, and after the client terminated, issue that command in Bash.
You should read about Bash pipelines - these are the way to run programs and input text inside them. Following your original idea to solving the problem, you'd write something like:
echo '\i auto_qa_db_sync.sql' | $DB_PATH -U $POSTGRESS_USER $Auto_rpt_production$TARGET_DB -p $TARGET_PORT
Hope that helps to understand.

zsh ssh-add -L parse error near `-L'

I'm trying to run
ssh-add -L
(or any other dashed option), and zsh returns zsh: parse error near `-L'. It's the first time I see zsh do that, and it doesn't do it with any other command.
Any ideas ?
First thing to find out is whether ssh-add is an alias or a shell function, rather than the binary executable /usr/bin/ssh-add.
Second, try to run the same command in a ZSH session without your custom ZSH configuration. To get a clean environment, run
env -i TERM=$TERM LC_ALL=$LC_ALL LANG=$LANG zsh -f
Then try ssh-add -L again and let us know what you see.
Moreover, please post the output of the following:
uname -a
zsh --version

how to set up the psql command in cygwin?

I have a local dev site on my machine with Apache server and PostgreSQL 9.1 database. As I'm using Windows, I also installed Cygwin. I want to access to database and make some queries via Cygwin insead of pgAdmin III, but it tells me that psql command not found. How should I set up the psql command in cygwin?
As of today, you just have to install postgresql-client package in cygwin:
Run your cygwin setup.exe file (this can be run multiple times to
add more packages).
Type postgresql into the search box, select postgresql-client and
press "next" to install.
Now you can open Cygwin terminal and type psql to run!
The best combo for Cygwin on Windows, I've found, is the normal Windows Postgres installation combined with Cygwin psql.
Cygwin psql (and other command-line tools) can be compiled from source fairly easily. Here's the steps for 9.2.4:
$ wget http://ftp.postgresql.org/pub/source/v9.2.4/postgresql-9.2.4.tar.bz2
$ tar xjf postgresql-9.2.4.tar.bz2
$ cd postgresql-9.2.4/
$ ./configure
$ cd src/bin/psql
$ make
This creates a psql.exe binary that works well with Cygwin. However, by default, it tries to connect to the local instance using a Unix socket instead of TCP. So use -h to specify the hostname and force TCP, for example:
$ ./psql -h localhost -U postgres
Move this psql.exe to someplace on your path (e.g. ~/bin) and possibly wrap in a script to add '-h localhost' for convenience when no other arguments supplied.
The source could be modified to change the default, but that takes actual work ;)
If I understand your question correctly you are running cygwin because you want to run queries against PostgreSQL via bash and psql on Windows, right?
Cygwin can run Windows binaries from bash, so install the native Windows builds and make sure psql.exe is in the PATH You should be able to copy the executable if necessary.
There is no need to install a native Cygwin build of PostgreSQL. Just use the existing psql tool, and make sure you can access the Windows-native psql.exe.