Get Current Command In Linux - linux

I'm creating a shell script that will always read the user's current commands from the shell.
I'm currently using the read command which I'm not satisfied with since it is used for prompting questions. Example:
root#hostname: ./script.sh
Question here?
answer - `read` command
I want the my script to be invoked when the user directly inputs a command on the command line (the script is already running through /etc/profile.d/myapp.sh once logged in).
root#hostname: read the command here
result will happen
My example script myapp.sh:
#!/bin/bash
if [ "input" = "value"]
then
do some actions
fi

If you are explicitly targeting Bash, I would recommend setting up Bash aliases for those commands.
At the start of the user's session, your program can run the following commands to create bash aliases:
alias command1="/your/path/script.sh command1"
alias command2="/your/path/script.sh command2"
Then for instance when the user enters command1, it will expand and run /your/path/script.sh command1 instead.

Related

What is the difference between interactive and non-interactive SSH commands? [duplicate]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a command that runs fine if I ssh to a machine and run it, but fails when I try to run it using a remote ssh command like :
ssh user#IP <command>
Comparing the output of "env" using both methods resutls in different environments. When I manually login to the machine and run env, I get much more environment variables then when I run :
ssh user#IP "env"
Any idea why ?
There are different types of shells. The SSH command execution shell is a non-interactive shell, whereas your normal shell is either a login shell or an interactive shell. Description follows, from man bash:
A login shell is one whose first character of argument
zero is a -, or one started with the --login option.
An interactive shell is one started without non-option
arguments and without the -c option whose standard input
and error are both connected to terminals (as determined
by isatty(3)), or one started with the -i option. PS1 is
set and $- includes i if bash is interactive, allowing a
shell script or a startup file to test this state.
The following paragraphs describe how bash executes its
startup files. If any of the files exist but cannot be
read, bash reports an error. Tildes are expanded in file
names as described below under Tilde Expansion in the
EXPANSION section.
When bash is invoked as an interactive login shell, or as
a non-interactive shell with the --login option, it first
reads and executes commands from the file /etc/profile, if
that file exists. After reading that file, it looks for
~/.bash_profile, ~/.bash_login, and ~/.profile, in that
order, and reads and executes commands from the first one
that exists and is readable. The --noprofile option may
be used when the shell is started to inhibit this behavĀ­
ior.
When a login shell exits, bash reads and executes commands
from the file ~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is
started, bash reads and executes commands from ~/.bashrc,
if that file exists. This may be inhibited by using the
--norc option. The --rcfile file option will force bash
to read and execute commands from file instead of
~/.bashrc.
When bash is started non-interactively, to run a shell
script, for example, it looks for the variable BASH_ENV in
the environment, expands its value if it appears there,
and uses the expanded value as the name of a file to read
and execute. Bash behaves as if the following command
were executed:
if [ -n "$BASH_ENV" ]; then . "$BASH_ENV"; fi
but the value of the PATH variable is not used to search
for the file name.
How about sourcing the profile before running the command?
ssh user#host "source /etc/profile; /path/script.sh"
You might find it best to change that to ~/.bash_profile, ~/.bashrc, or whatever.
(As here (linuxquestions.org))
Shell environment does not load when running remote ssh command. You can edit ssh environment file:
vi ~/.ssh/environment
Its format is:
VAR1=VALUE1
VAR2=VALUE2
Also, check sshd configuration for PermitUserEnvironment=yes option.
I had similar issue, but in the end I found out that ~/.bashrc was all I needed.
However, in Ubuntu, I had to comment the line that stops processing ~/.bashrc :
#If not running interactively, don't do anything
[ -z "$PS1" ] && return
I found an easy resolution for this issue was to add
source /etc/profile
to the top of the script.sh file I was trying to run on the target system.
On the systems here, this caused the environmental variables which were needed by script.sh to be configured as if running from a login shell.
In one of the prior responses it was suggested that ~/.bashr_profile etc... be used.
I didn't spend much time on this but, the problem with this is if you ssh to a different user on the target system than the shell on the source system from which you log in it appeared to me that this causes the source system user name to be used for the ~.
Just export the environment variables you want above the check for a non-interactive shell in ~/.bashrc.

How to know the default shell process for my terminal? [duplicate]

This question already has answers here:
How to get the default shell
(5 answers)
Closed 6 years ago.
I am composing a bash script to serve as a utility tools.
The challenges I am facing now is:
- user using my tool will be running in bash environment
- however, some of them might default using krcsh or tcsh. they might have aliases or configurations set in there.
So, I need to prompt/guide user to resolve this during installation. My first challenge: How am I suppose to know the user's default shell within my install.sh?
Knowing the "default" shell, I can prompt and guide the user to do necessary transfer to bash.
my testing code:
my result:
1/ is fault obviously. It return the current shell which is my install.sh (bash)
2/ I am doubtful. It seems to be the history of what I have run before. It does not show me my default configured shell. My case, my terminal default shell is bash, and I run tsch for testing purpose. So the script parsed wrong information and will though my default shell is tcsh. It will then assist me to port configurations from tcsh to bash during the installation process.
If you want to check the shell you are using, you can use the following methods:
echo $0 in terminal will show you the program running if you want to check the shell you are currently using.
echo $SHELL - with this command you can read the user's default shell in the terminal you are running.
If you want to prompt, easily you can put the echo $SHELL in the part of your script where you need to show the current shell you are using.
Don't forget to put #!/bin/bash if your script is designed to run in a bash shell!

How to supply an input value to the prompt via shell script?

I am writing a wrapper shell script wrapper.sh to run bunch of other already available scripts owned by other people and I cannot touch those scripts.
The problem is, there is one script that runs some db specific activities - db_perf_clean.sh. That script is normally executed manually and it prompts for a password at run time. There is no way I can supply the password to it as a parameter and I cannot modify that script. As such I know the db password and I can provide it in wrapper.sh.
Please let me know how can I run that db_perf_clean.sh script inside wrapper.sh like in a silent mode.
Sometimes a script will insist that a password be read from the tty. Often, it will read from stdin. If so, try:
echo password | db_perf_clean.sh
The above has the disadvantage that the password will appear in ps. To avoid that, hide the password in a file and use that file for stdin:
db_perf_clean.sh <file_with_password
If you want the command to be silent, you can throwaway its output:
db_perf_clean.sh <file_with_password >/dev/null 2>&1
Under bash, as opposed to generic shell, that can be slightly simplified:
db_perf_clean.sh <file_with_password &>/dev/null
I found out little different approach instead of writing a password in a file and that worked too ->
db_pass="somevalue"
sh db_perf_clean.sh<<EOM
$db_pass
EOM

read user input without interruption in bash

I'm trying to grep the user command into a variable.
If the command begins with specific words I'm redirecting the command into another shell (not BASH), if not it will run regularly on bash.
When I'm using "read" I lose the BASH prompt then I'm unable to auto-complete, backspace etc.
My goal is:
if I type for example ls -la the command will not run and will be assigned into variable:
user#machine:~$ ls -la
user_command = 'ls -la'
if the command is not other shell command (not BASH)
eval $user_command
Is there any way to achieve this?
I guess you want to : invoke some specific commands through another shell instead of bash
You could define a function for each of those commands
exemple:
ls () { # function that, instead of bash, launch 'ls' through ksh
ksh -c "ls $*"
}
that way, with each function's name matching the command you want to run with another shell (edit the inside of the function accordingly if it needs to be run with another shell than ksh)
Place those function in specific user's "~/.bashrc" files.
You could write your implementation of ls and place it in ~/bin/ls.
Give full permissions to this file

Difference between different ways of running shell script

Recently I have been asked a question. What are the different ways of executing shell script and what is the difference between each methods ?
I said we can run shell script in the following methods assuming test.sh is the script name,
sh test.sh
./test.sh
. ./test.sh
I don't know the difference between 1 & 2. But usually in first 2 methods, upon executing, it will spawn new process and run the same. Whereas in the last method, it won't spawn new process. Instead it runs in the same one.
Can someone throw more insight on this and correct me if I am wrong?
sh test.sh
Tells the command to use sh to execute test.sh.
./test.sh
Tells the command to execute the script. The interpreter needs to be defined in the first line with something like #!/bin/sh or #!/bin/bash. Note (thanks keltar) that in this case the file test.sh needs to have execution rights for the user performing this command. Otherwise it will not be executed.
In both cases, all variables used will expire after the script is executed.
. ./test.sh
Sources the code. That is, it executes it and whatever executed, variables defined, etc, will persist in the session.
For further information, you can check What is the difference between executing a bash script and sourcing a bash script? very good answer:
The differences are:
When you execute the script you are opening a new shell, type
the commands in the new shell, copy the output back to your current
shell, then close the new shell. Any changes to environment will take
effect only in the new shell and will be lost once the new shell is
closed.
When you source the script you are typing the commands in your
current shell. Any changes to the environment will take effect and stay in your current shell.

Resources