How to use background execute in bashrc with alias? - shell

I want to replace complex commands with simple characters and execute in background, so I can shut down the terminal.
The command is nWave -ssf test.fsdb(fsdb is a wavefile). In bashrc, I write alias nwave='nWave -ssf'.
If I want the command to be executed in background, I use nWave -ssf test.fsdb &. How to realize this command when using alias in bashrc file.

If you plan to close the terminal, you probably want to wrap the command in nohup. However, using 'nohup alias ...' will not work - as nohup only accept regular command. You can embed the nohup into the alias explicitly.
# In bashrc
alias bgnwave 'nohup nWave -ssf'
# At the terminal
bgnware test.fsdb &
As an alternative to bash alias, consider using bash function
function bgwnwave {
nohup nWave -ssf "$#" &
}

Related

bash interact just once

I want to write a script for Ubuntu, which open a terminal-emulator, which only allows users interact with it only once. After finish running user's first command typed in, the terminal close on itself automatically, which is kind of like Win+R on windows OS.
How should I do that?
I try script like gnome-terminal -- bash -c "read cmd && $cmd", but there's two problem:
No auto-complete on user inputting commands;
Commands from .bashrc, .bash_aliases are not recognized.
You can try :
gnome-terminal -- bash --rcfile <(cat ~/.bashrc; echo 'PROMPT_COMMAND="PROMPT_COMMAND=exit"')
I don't have Ubuntu to test at the moment, but bash ... part worked.

Open new terminal with bash script and use it

I would like to run a python script multiple times in a new terminal each time so I am making a bash script to do this.
#!/bin/bash
alias bot_1="cd ../Folder1"
alias bot_2="cd ../Folder2"
gnome-terminal
bot_1
python3 bot_one.py
gnome-terminal
bot_2
python3 bot_two.py
I would like to run a python script multiple times in a new terminal each time so I am making a bash script to do this.
With my script I have a new terminal which opens but the following commands are executed in the old and not the new
gnome-terminal has the ability to execute a command other than the default interactive shell.
gnome-terminal --working-directory ../Folder1 -- python3 bot_one.py
gnome-terminal --working-directory ../Folder2 -- python3 bot_two.py

How can I open shell and then execute a command inside it

What I want is to open default shell, then call another and execute a command there.
Was trying something like this:
c:/Windows/System32/bash.exe -c "zsh & zstyle"
or
cmd /k "c:/Windows/System32/bash.exe -c zsh" & zstyle - this open shell but doesn't run a commands
or
c:/Windows/System32/bash.exe -c "zsh -c 'zstyle'"
Currently I am using a cmder/conemu terminal for windows.
Unfortunately, passing a startup to command to zsh with -c and keeping it open for interactive use (with -i) doesn't work.
Disclaimer: The following solutions were tested from a regular Command Prompt (cmd.exe), not cmder/conemu, though I'd expect them to work there too.
To try them from PowerShell (v3+), insert --% as the first argument after (right after bash.exe).
Here's a workaround:
c:/Windows/System32/bash.exe -c "zsh -c 'zstyle' && exec zsh -i"
Note that command zstyle is executed in a different, transient zsh instance, so this approach won't work for commands whose purpose is to modify the environment of the interactive shell that stays open.
If that is a requirement, things get more complicated (this solution courtesy of this answer):
c:/Windows/System32/bash.exe -c "{ { echo 'zstyle'; echo 'exec 0<&3-';} | zsh -i; } 3<&0"
Note, however, that both commands being executed will be printed before their output, if any, is shown, preceded by the prompt - as if the commands had been typed interactively.

Vim run commands from bash script and exit without leaving shell in a bad state

If I do:
echo 'vim +BundleInstall +qall' | bash
it installs my bundles correctly, but leaves the shell in a bad state (ncurses options) because of the pipe.
Is there a way to prevent the shell from being in a bad state?
Same for the minimal test case: echo 'vim +qall' | bash
Similar to: Run vim command from commandline, but the question there was for an interactive shell, so vim +BundleInstall +qall was fine.
I want to do this to be able to automate Vim plugin installation as:
wget -O- http://a.com/bootstrap-scrit.sh | bash
in a bootstrap script that currently contains vim +BundleInstall +qall. This command can be changed if needed.
Vundle issue: https://github.com/gmarik/Vundle.vim/issues/59
You can source your script instead, like so:
. <(wget -O- http://a.com/bootstrap-scrit.sh)

How to source a csh script from inside a bash script

My default shell is bash. I have set some environment variables in my .bashrc file.
I installed a program which use .cshrc file. It contains the path to several cshell scripts.
When I run the following commands in the shell windows it works perfectly :
exec csh
source .cshrc
exec bash
I have tried to put these commands in bash script, unfortunately it didn't work.
is there another way to write a script in order to get the same result as running commands from a shell windows.
I hope my question is now clear
Many thanks for any help
WARNING : don't put the following script in your .bashrc, it will reload bash and so reload .bashrc again and again (stopable with C-c anyway)
Use preferable this script in your kit/CDS stuff startup script. (cadence presumably)
WARNING 2 : if anything in your file2source fails, the whole 'trick' stops.
Call this script : cshWrapper.csh
#! /bin/csh
# to launch using
# exec cshWrapper.csh file2source.sh
source $1
exec $SHELL -i
and launch it using
exec ./cshWrapper.csh file2source.sh
it will : launch csh, source your file and came back to the same parrent bash shell
Example :
$> ps
PID TTY TIME CMD
7065 pts/0 00:00:02 bash
$>exec ./cshWrapper.csh toggle.csh
file sourced
1
$> echo $$
7065
where in my case i use the file toggle.csh
#! /bin/csh
# source ./toggle.csh
if ! $?TOGGLE then
setenv TOGGLE 0
endif
if ($?TOGGLE) then
echo 'file sourced'
if ($TOGGLE == 0) then
setenv TOGGLE 1
else
setenv TOGGLE 0
endif
endif
echo $TOGGLE
Hope it helps
New proposal, since I faced another problem with exec.
exec kills whatever remains in the script, except if you force a fork by using a pipe after it `exec script |cat'. In such case if you have environment variable in the script, they are not spread back to the script itself, which is not what we want. The only solution I found is to use 3 files (let's call them for the example : main.bash that call first.cshrc and second.sh).
#! /bin/bash
#_main.bash_
exec /bin/csh -c "source /path_to_file/cshrc; exec /bin/bash -i -c /path_to_file/second.sh"
# after exec nothing remains (like Attila the Hun)
# the rest of the script is in 'second.sh'
With that manner, i can launch in a single script call, an old cshrc design kit, and still process some bash command after, and finally launch the main program in bash (let say virtuoso)

Resources