How to use 'history-c' command in a bash script? [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 7 years ago.
Improve this question
As we know, 'history' command displays the command line history of Linux server and 'history -c' is the command to clear/delete this command line history.
I have to trigger this command through my bash script. Script is as follows,
#! /bin/bash
var=`history -c`
if [ $? -eq 0 ]
then
echo "cleared"
echo $var
fi
Output is as follows:
cleared
Though its printing "cleared" as the output, history-c is not deleting the history.
It would be great if you can guide/suggest on how i can achieve this, i.e using "history-c" command in my bahs script to delete command line history.Or is there any other way in which i can delete command line history through my bash script.
Thanks & Regards,
Navya

history -c clears the history for the current shell, and does not delete ~/.bash_history.
But when you run a script the current shell creates a new shell to run the script in and exits that shell when the script is done.
Instead, to execute a script in the current shell you have to source the script. If the name of your script is foo.sh, try running . ./foo.sh
But in either case, the script that you've written does not execute the command. Modify it to something like this:
#! /bin/bash
history -c
if [ $? -eq 0 ]
then
echo "cleared"
fi

Related

How to reset bash config? [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 2 years ago.
Improve this question
Previously, when I mistakenly entered a wrong command in the terminal, I would get an output like this:
Command 'whoaim' not found, did you mean:
command 'whoami' from deb coreutils (8.30-3ubuntu2)
Try: sudo apt install <deb name>
but now when I enter a wrong command like whoaim instead of whoami I get this output:
bash: whoaim: command not found
how should I reset bash config?
You can reset the bash profile of user using following commands:-
Overwrite the existing .bashrc from user's home directory.
cp /etc/skel/.bashrc ~/
source ~/.bashrc
Take backup of existing .bashrc from user's home directory
However, check here how you can use the utility command-not-found in ubuntu
You would need to call a function that gives bash this capability. It's typically called command_not_found_handle. First, append it to your .bashrc file. Here is the code:
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1";
return $?;
else
if [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1";
return $?;
else
printf "%s: command not found\n" "$1" 1>&2;
return 127;
fi;
fi
}
And then run this command to reload the profile file:
. ~/.bashrc

How to programmatically trigger actions when last Linux terminal is exited/killed? [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 2 years ago.
Improve this question
I've modified my .bashrc to mount directories when I start first terminal after login.
If mount point still exists when I start new terminal nothing is done.
Now I want to add a bit of code when last linux terminal is closed/exited (e.g. umount those directories, etc..)
Also there is this not-so-intelligent way of discovering how many terminal instances are still running:
ps -au | grep "bash" | grep "grep" -v -c
I'm running Ubuntu 20.04. and I'm using bash shell.
Questions:
Is there a file which is "triggered" on terminal exit just like .bashrc is on terminal startup? I've tried messing around with .bash_logout but it doesn't seem to do anything in my case (echo, touch..)
Is there another way to do what I'm trying to achieve which doesn't include file from question #1 (if such file even exists)?
You can get it done with the help of trap which is a shell builtin.
For example if you want to clear a folder on running exit command in bash >
trap "rm /cache/*" EXIT
The syntax should be like trap <command> <SIGNAL>
Just put this in the bottom of ~/.bashrc with your desired command and it should run before the terminal is killed.
Try trap --help to know more.

What is the advantage of using bash -c over using a here string [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 5 years ago.
Improve this question
Is there any real benefit to using bash -c 'some command' over using bash <<< 'some command'
They seem to achieve the same effect.
bash -c '...' leaves you the option to provide stdin input to the command,
whereas bash <<<'...' precludes that option, because stdin is already being used to provide the script to execute.
Examples:
# Executes the `ls` command then processes stdin input via `cat`
echo hi | bash -c 'ls -d /; cat -n'
/
1 hi
# The here-string input takes precedence and pipeline input is ignored.
# The `ls` command executes as expected, but `cat` has nothing to read,
# since all stdin input (from the here-string) has already been consumed.
echo hi | bash <<<'ls -d /; cat -n'
/

#!/bin/ksh not working in redhat linux [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
I am using the command:
#!/bin/ksh
as the first line in my shell script (hopefully this will dictate the shell to execute in the KSH Shell) . But when i am executing my shell like below it is not actually happening.
sh test_shell.txt
But when i using like below with or with out (#!/bin/ksh) i am getting proper output.
ksh test_shell.txt
In my code i am setting a value to variable like below.
set -A variable_name
But i think -A is not recognized in sh where as it is working as an assignment in ksh shell.
How can I resolve this?
When you execute
sh test_shell.txt
then you are launching the sh (Bourne) shell with your file name as the script to run. In this case the first line of the script is ignored because it is a comment. The rest of the script is expected to be in Bourne shell syntax, because that's the shell you requested.
If you set your script to executable (chmod +x test_shell.txt) and then run it with:
./test_shell.txt
then the kernel will read the first line of the script, see it starts with the special #!, and run your script with /bin/ksh.

Copy a line from text file to 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 8 years ago.
Improve this question
Hello dear smart people,
If I'm in a login linux shell and I'm browsing a text file full of lines with vim or cat.
example
mar#001:~$ cat test
sudo kill pluto
sudo echo $Parerino
sudo cut -d -f--d-f a -sdf-s-f 2> myerr
sudo kill pluto
sudo echo $PATH
sudo echo $Parerino
How is possible to do the copy of the "sudo cut -d -f--d-f a -sdf-s-f 2> myerr" command, after I closed the file and paste it in the shell so then I can execute it? or may I do it automatically?
How can I do that copy of that command that I need then open a new shell and paste the long command to modify it before to execute it?
Since you most likely don't have a clipboard you won't be able to copy/paste from Vim to the shell.
But you can yank the current line and execute it with :!:
0y$
:!<C-r>"<CR>
Or you can execute the current line as returned by getline:
:!<C-r>=getline('.')<CR><CR>
Or you can write the line into a file and execute its content:
:.w foo
<C-z>
$ cat foo | sh
Or you can remember the line of your command, quit Vim and do:
sed -e 5p | sh
If no GUI at all, then I can only think of gpm and screen. You may want to take a look at the following thread: https://superuser.com/questions/67644/linux-copy-paste-in-tty

Resources