How to call multiple bash command and ssh in Ruby? - ruby-on-rails

I want to do the following commands in ruby.
ssh into another computer using ssh example#example
set source file source ~/.profile
cd to/some/folder
call my shell script with parameters, a json formatted string ,./my_script.sh my_hash.to_json
However I am facing these problems:
I call them in one line using backticks, it works, but it is a very bad practice in my opinion because it is not readable nor it is maintainable.
On the other hand, when I call my_hash.to_json, the resulted string has non-escaped double quotes, How do I escape them?

I would recommend to view this tutorial for ssh with ruby. then make a shell script and move it to server and then execute like a single command.
create a single shell script file for example script1 and then execute it at once instead of executing each command individually.
open file script1 using any editor.
copy all commands to script1 (each command in new line).
script1 file should look like this
#!/bin/bash
ssh example#example
source ~/.profile
cd to/some/folder
save file
make this file executable using chmod +x script
execute it in ruby like this [backtick]./script1[backtick]
note: copy script1 to usr/bin to avoid "./" and then try command only script1.
Reference for passing arguments in shell script is here.

Related

Run shell script file rails controller with argument

I have a shell script file in the rails application.Can anyone please tell me how can i call the shell script file with argument in my rails controller?
Thanks
Try backticks (``). For example a = `date` would assign shell's date command output to the a variable.
UPD: And you can obviously run something like `~/workspace/my_script.sh` to execute a script from a file, just make sure you give it executable permission first (with chmod +x ~/workspace/my_script.sh).
UPD2: And for the parameters, you can just interpolate them, like so `~/workspace/my_script.sh #{my_parameter_one}`. my_parameter_one will be available in the script as $1.

Alpine not loading /etc/profile [duplicate]

I'm trying to write (what I thought would be) a simple bash script that will:
run virtualenv to create a new environment at $1
activate the virtual environment
do some more stuff (install django, add django-admin.py to the virtualenv's path, etc.)
Step 1 works quite well, but I can't seem to activate the virtualenv. For those not familiar with virtualenv, it creates an activate file that activates the virtual environment. From the CLI, you run it using source
source $env_name/bin/activate
Where $env_name, obviously, is the name of the dir that the virtual env is installed in.
In my script, after creating the virtual environment, I store the path to the activate script like this:
activate="`pwd`/$ENV_NAME/bin/activate"
But when I call source "$activate", I get this:
/home/clawlor/bin/scripts/djangoenv: 20: source: not found
I know that $activate contains the correct path to the activate script, in fact I even test that a file is there before I call source. But source itself can't seem to find it. I've also tried running all of the steps manually in the CLI, where everything works fine.
In my research I found this script, which is similar to what I want but is also doing a lot of other things that I don't need, like storing all of the virtual environments in a ~/.virtualenv directory (or whatever is in $WORKON_HOME). But it seems to me that he is creating the path to activate, and calling source "$activate" in basically the same way I am.
Here is the script in its entirety:
#!/bin/sh
PYTHON_PATH=~/bin/python-2.6.1/bin/python
if [ $# = 1 ]
then
ENV_NAME="$1"
virtualenv -p $PYTHON_PATH --no-site-packages $ENV_NAME
activate="`pwd`/$ENV_NAME/bin/activate"
if [ ! -f "$activate" ]
then
echo "ERROR: activate not found at $activate"
return 1
fi
source "$activate"
else
echo 'Usage: djangoenv ENV_NAME'
fi
DISCLAIMER: My bash script-fu is pretty weak. I'm fairly comfortable at the CLI, but there may well be some extremely stupid reason this isn't working.
If you're writing a bash script, call it by name:
#!/bin/bash
/bin/sh is not guaranteed to be bash. This caused a ton of broken scripts in Ubuntu some years ago (IIRC).
The source builtin works just fine in bash; but you might as well just use dot like Norman suggested.
In the POSIX standard, which /bin/sh is supposed to respect, the command is . (a single dot), not source. The source command is a csh-ism that has been pulled into bash.
Try
. $env_name/bin/activate
Or if you must have non-POSIX bash-isms in your code, use #!/bin/bash.
In Ubuntu if you execute the script with sh scriptname.sh you get this problem.
Try executing the script with ./scriptname.sh instead.
best to add the full path of the file you intend to source.
eg
source ./.env instead of source .env
or source /var/www/html/site1/.env

Run script on start lua interpreter

How i can automatically run some script on lua without bat/shortcut, when i start interpreter? M.b. lua have some reserved file names like autorun or autoexec?
Just skip following text. Its for validator.
For portable use you can create bat file. Its dont like to me, but it works:
luajit.exe %~dp0%~n0.lua
that .bat run script with same name.
my_name.bat - run my_name.lua
123.bat - run 123.lua
Its not perfect but idk another decision.

Extend $PATH variable in git bash under Windows

I'm trying to extend my $PATH variable in git bash (MinGW shell) by adding the following to the file ~/.bashrc
PATH=$PATH':/c/Program Files/maven/apache-maven-3.2.5/bin'
After I did this and restarted the bash it seems like that the $PATH variable was extended like expected:
$ echo $PATH
MANY_OTHER_PATHS:/c/Program Files/maven/apache-maven-3.2.5/bin
But I still cannot execute the programms in the given directory:
$ mvn
bash: mvn: command not found
What went wrong here? How do I extend the PATH variable correctly?
Here are two ideas.
You can have your path with double quote mark.
export PATH=$PATH:"/C/Program Files (x86)/apache-maven-3.3.3/bin"
Or, You can also make symbolic link for the directory.
ln -s "/C/Program Files (x86)/apache-maven-3.3.3/bin" ./mvnbin
export PATH=$PATH:/your-path/mvnbin
It works for me in mingw32 environment.
I needed to add something to my Git Bash path permanently each time I open it. It was Meld.exe path which can be added with:
export PATH=$PATH:"/C/Program Files (x86)/Meld/lib"
In order to execute this command each bash session, you need a ~/.bashrc file. Check if it already exists or create it using notepad ~/.bashrc or touch ~/.bashrc.
You can check where it is with:
echo ~
Open it and add the command that adds the PATH (first command in this response).
I hope you found this useful.
According to this SO post, you need to escape Program Files with quotes. git-bash $PATH cannot parse windows directory with space
Add PATH in Git Bash Permanently | Windows Only
Just in case you are still wondering how to add a path permanently in git bash here is the step-by-step process for Windows users:
Create .bashrc in user's root folder using the below command. It will open notepad and ask you to create the file, click yes.
notepad ~/.bashrc
Put the directory you want to add as below, for more than 1 items repeat the same format in next line:
export PATH=$PATH:"/c/folder/folder/"
Save the file and relaunch the bash.
Next launch will give you a warning like WARNING: Found ~/.bashrc but no ~/.bash_profile, ~/.bash_login or ~/.profile. but git bash will handle it by creating the required files.
SOME INSIGHTS
Git Bash doesn't fetch Window's environment PATH, it maintains its PATH separately in more like a Linux way.
You can run export PATH=$PATH:"/c/folder/folder/" in cmd to add a directory to path, but it will be only for the current session once you close the bash, it will be gone.
.bashrc is a shell script file that will be executed every time you launch a new git bash window. So you can add any type of bash command here. We simply added the export command to add our desired directory to PATH.

Setting path in Solaris Sparc

I have one python command file, i want to set it as a PATH in Solaris Sparc so that i can easily use my command from anywhere. For example the file name is abc.py and it contains abc --version to display version of file abc. So, after opening terminal i should only give command abc --version and it should display version of abc.
The architecture (SPARC) has nothing to do with the PATH which is more a shell thing but you do not tell what shell you are using.
Anyway, if you use a bourne style shell, i.e. not csh/tcsh, and you don't mind this to affect every user account on that host, you might add the wanted path to the PATH setting in the file /etc/profile.
When abc.py is located in your homedir, you can start it with ~/abc.py.
You need to call the file abc.py with abc.py, not abc (and have a shebang line which instructs the shell where it can find python).
When you want to start the file with ./abc, you can rename the file to abc (the shebang will tell it is python, not the .py), or introduce an alias:
alias abc="~/abc.py"
Using an alias can be an alternative for adding a shebang line:
alias abc="/usr/bin/python abc.py"
When you do not want to use an alias you can make a bin dir and put abc there.
I will add the shebang for you:
mkdir ~/bin
echo "#!/usr/bin/python" > ~/bin/abc
cat abc.py >> ~/bin/abc
chmod +x ~/bin/abc
mv abc.py bin/abc.py.old
Now change your login PATH with PATH=${PATH}:$HOME/bin in your .profile or .bashrc, and login again (or source the login script).

Resources