Using bash alias beyond one level (shell in shell) [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I am looking to execute a alias on my terminal which will login to another shell and execute a command there.
For example,
sh = 'ssh admin#x.x.x.x'
shls = 'sh;ls' (also tried 'sh && ls')
In this scenario, when i give 'shls', i want to ssh to (pwd less entry enabled) x.x.x.x and then execute ls command over there. But the 'ls' part is not working.
I understand the shell changed and hence its no longer in parent shell's scope to trigger the ls, but just wondering if there is a way to push it to the logged in shell and execute there.
Infact, i wanted to use another alias which is avaiable in x.x.x.x in place of 'ls' but as a first step i want to atleast get this working.
Hope i could put it clearly, Thanks in advance for your help.

You can pass a command to ssh as an argument (after the various connection parameters):
alias shls='ssh admin#x.x.x.x ls'
BTW, I'd recommend against aliasing sh -- that's a commonly used command to run a shell script(*), and giving it a different meaning could cause confusion.
(* Though instead using the sh command, it's generally better to give the script a proper shebang line, and just enter its path.)

Related

Alternatives to eval `ssh-agent` and exec ssh-agent bash [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
Running either of these commands seems to start an SSH agent process successfully:
eval `ssh-agent`
OR
exec ssh-agent bash
I'm partial to the first one, because the second exec replaces the shell. Obviously the second, uses eval which is frowned upon by some, but I don't see alternatives.
My questions are:
Does exec have any negative side effects when replacing the shell or indeed any side effects at all? Are my concerns about using exec warranted?
I don't have an issue using eval but, out of interest what alternative commands are there without scripts or functions (and without exec or eval) to start an ssh-agent process in one line?
This is a "safe" use of eval, at least to the extent that you trust ssh-agent to output nothing but simple, hard-coded assignments similar to
SSH_AUTH_SOCK=/var/folders/...; export SSH_AUTH_SOCK;
SSH_AGENT_PID=xxxxx; export SSH_AGENT_PID;
echo Agent pid xxxxx;
The output of ssh-agent is specifically designed to be passed to eval, and let's face it: if ssh-agent wanted to do harm, it could do so in a quieter fashion.
The downside to using exec is that the new shell that replaces the original shell may not be identical; the environment is inherited, but some shell settings not found in .bashrc may be different. However, if you put exec ssh-agent bash in your .bashrc (especially as the last line), there there isn't really any opportunity for your shell's configuration to diverge from whatever .bashrc did. (There is also the possibility that you have non-idempotent code in your .bashrc, meaning that executing it twice will result in different behavior than having only executed it once. But again, that's unlikely and easily auditable.)

Run multiple commands in serial and make sure all commands run [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I want to run a series of unix commands, one after another. If any of these commands dies for whatever reason, subsequent commands should continue to run.
For instance, I have 3 commands called "setup", "long-running-job" and "teardown". If "long-running-job" finishes with whatever exit code, or dies unexpectedly, I want to make sure "teardown" gets run in any case.
Simply concatenating all commands with semicolons doesn't seem to work. I tried running touch test.txt; ping localhost; rm test.txt in macOS Terminal, closed the terminal tab while it's running, and found that the "test.text" didn't get removed.
If you want to make sure that even if your interactive window closes that your commands keep going and that all your command happen sequentially in the order you specify then use the method in this answer: https://unix.stackexchange.com/a/47231
Basically for the command line in your question
nohup sh -c 'touch test.txt; ping localhost; rm test.txt'
That means the hang up signal sent by closing the terminal is ignored.
Have you tried GNU parallel? Seems like the ideal tool for your needs.
$> parallel ::: setup long-running-job teardown
Parallel comes with tons of options to control halts, failures, jumps, retries, etc. See the manual and the tutorial for examples.

Load some alias just after SSH login [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
In my daily work I often need to use a ssh connection on a device (which you can consider as read only), and the commands I write are long.
That's why I would like to load some alias just after ssh login.
But when I try something as follow, it don't works:
ssh name#ipAdress "bash -l ; alias short='veryLongCommandThatIWriteOften'"
I guess that's because bash stop the processing of the other commands which are just after.
So is it possible to set aliases directly as an argument of bash, or is there another solution to do what I want?
Instead of an alias, you can use a shell function, which bash allows you to export. This way, you first define the function, then export its name, and finally start a new interactive shell which inherits your function. For example:
ssh -t name#ipAddress "short () { veryLongCommandThatIWriteOften; }; export -f short; bash"
The -t is necessary to set up the pseudo terminal for the interactive bash shell, as ssh won't do it automatically for an apparently non-interactive command.
Note that you many need to be careful about quoting, depending on what the body of short is.
Edit the file ~/.bashrc
nano ~/.bashrc
It has examples of how to set aliases also. Log back in to have new alias work.

Is it possible to update a varible from one shell to another shell? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 9 years ago.
Improve this question
How can we update a variable from one shell to another shell ?
Suppose , I am having 2 Putty sessions opened , I want to set a variable in the first SHELL and I need that variable to access from the 2nd SHELL .
Is is possible ?
You can save the variable to a script.
Then source the script in the 2nd session.
For example:
# session 1
hello=world
echo "hello=$hello" > /tmp/var.sh
# session 2
. /tmp/var.sh
echo $hello
As each process' environment is protected, there's no way to share environment variables. I would suggest using a file on a shared filesystem to store the variable you want and reading that file in whenever you'd need to know what the new value is.
It is usually not possible, because each shell (and each process) has its own environment. See execve(2).
However, you might want to switch to the fish shell. It gives you so called universal variables which might be shared between several instances of (i.e. processes running) the fish shell. This is implemented thru the fishd user daemon (with which every fish process communicates).

Need to determine when linux/OSX is fully initialized [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
This question does not appear to be about programming within the scope defined in the help center.
Improve this question
I need a shell scripting way to determine when a remote linux/OSX has completed all /etc/init.d/* for what ever run levels I have chosen to run in.
since I know I can ssh into a virutal box on my net while it is still booting and run commands like /bin/true or /bin/ps that not everthing is in the state it needs to be.
essentially I need to
while [ ${INIT_STILL_RUNNING_ON_REMOTE_SYSTEN} ]
do
sleep 30
done
ssh remoteUser#remoteSystem:command
You could put a script in the run-level you are interested in that is the last thing to run and first thing to shut down that touches a file somewhere to indicate it has run.
For instance, if you made a script called S99finished and put it in the run-level folder it would run last at that run-level. A corresponding K00finished would run first when shutting down.
S99finished could look something like:
#!/bin/bash
touch ~/.init_finished
and K00finished could look something like:
#!/bin/bash
if [ -e ~/.init_finished ]; then
rm ~/.init_finished
fi
Then your startup script would poll until ~/.init_finished existed at which point it would go on it's merry way.
Note, the startup scripts run as root, so using the home directory tilde will put it in root's home. That's probably not ideal for what you're doing, but illustrates the point. It's just as easy to put it in /var/log or somewhere else common to poll from. Just remember it has to have read permissions for everybody wherever you stick it.

Resources