User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Load same history every time

Fri Oct 05, 2018 3:44 am

I'm trying to have a pre-defined list of commands in history each time I start terminal so I can quickly do a few tasks with the up arrow and enter buttons only. I tried this in .bashrc but that didn't work at first:

Code: Select all

history -c;history -r /home/pi/Documents/cmd_history.txt
Whatever I did before closing the terminal still shows up in history and the commands didn't seem to work. So I made some changes:

Code: Select all

cat /dev/null >.bash_history
history -c;history -r /home/pi/Documents/cmd_history.txt
This time it works. Terminal starts with the same clean list of commands I want to use each time.

Any better way to do this? Are you looking to achieve the same goal?
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

W. H. Heydt
Posts: 10577
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Load same history every time

Fri Oct 05, 2018 5:01 am

Why not just write a shell script that runs the sequence of commands you want? You could then either run the shell script manually when you long in, or you could add it to your autostart file and it would run every time you log in.

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Load same history every time

Fri Oct 05, 2018 5:20 am

Background: The RPI is sealed in a waterproof box with a display and a few push buttons around the box's sides. It's to be used in the field where people may wear gloves and have water and dust and probably grease. They need to select what command to run and not have to run them in any particular sequence.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

bertlea
Posts: 274
Joined: Wed Dec 07, 2016 6:33 am
Location: Hong Kong

Re: Load same history every time

Fri Oct 05, 2018 6:42 am

Include the command to run your “clean-up history” script in your predefined command history. Then the user can run that command anytime when he/she feels there is a need.

If you need to have the command history always only remember the predefined commands, nothing else. Then you may create a script for each and all of the commands you needed and add your “clean-up history” sequence at the end of each command script. Assume nobody will run anything outside your list of predefined command scripts, your command history should always be the same as you designed.

tpyo kingg
Posts: 562
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Load same history every time

Fri Oct 05, 2018 6:50 am

Rather than overwrite .bash_history another way would be to unset the $HISTFILE variable:

Code: Select all

unset HISTFILE;
history -r /home/pi/Documents/cmd_history.txt
It's fewer steps but that may not matter so much.

W. H. Heydt
Posts: 10577
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Load same history every time

Fri Oct 05, 2018 1:52 pm

liudr wrote:
Fri Oct 05, 2018 5:20 am
Background: The RPI is sealed in a waterproof box with a display and a few push buttons around the box's sides. It's to be used in the field where people may wear gloves and have water and dust and probably grease. They need to select what command to run and not have to run them in any particular sequence.
Okay... This is why we ask people to ask complete questions. I would suggest that you create a display that associates the various buttons with the different programs you want to run (a menuing system) rather than mucking about with the history file.

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Load same history every time

Mon Oct 08, 2018 2:20 pm

bertlea wrote:
Fri Oct 05, 2018 6:42 am
Include the command to run your “clean-up history” script in your predefined command history. Then the user can run that command anytime when he/she feels there is a need.

If you need to have the command history always only remember the predefined commands, nothing else. Then you may create a script for each and all of the commands you needed and add your “clean-up history” sequence at the end of each command script. Assume nobody will run anything outside your list of predefined command scripts, your command history should always be the same as you designed.
Great idea! Then the operator can select numbers with number entry button in the script. I'll keep that in mind and compare with history recall to see which one is easier to use. The "clean-up history" command/script is also a good idea. I'll mark it as "restore history" or something that sounds like restoring the predefined list.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Load same history every time

Mon Oct 08, 2018 2:24 pm

tpyo kingg wrote:
Fri Oct 05, 2018 6:50 am
Rather than overwrite .bash_history another way would be to unset the $HISTFILE variable:

Code: Select all

unset HISTFILE;
history -r /home/pi/Documents/cmd_history.txt
It's fewer steps but that may not matter so much.
Thanks. I'll try this as well. Will this have to be done just once per user account or included in .bashrc?
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

tpyo kingg
Posts: 562
Joined: Mon Apr 09, 2018 5:26 pm
Location: N. Finland

Re: Load same history every time

Mon Oct 08, 2018 2:54 pm

That would go in the user's .bashrc file.

grininmonkey
Posts: 145
Joined: Mon Jul 30, 2018 3:44 pm

Re: Load same history every time

Tue Oct 09, 2018 3:49 pm

Thought I would throw this out there in case it might inspire a different approach than managing history.

The following is an approach I took for my thermostat project where a 5" hdmi screen is on the wall with three buttons on GPIO pins which mimic keyboard UP, DOWN and ENTER keys. This approach also gives me remote screen control when I ssh to the pi from another device/computer... as in what I see in my terminal is what is visible on the 5" display. In my project, I setup a fancy Fallout 4 Terminal using curses... however I will use a whiptail example as it requires a lot less knowledge and is also available by default on raspian images... including lite.

Note: This is for when your pi is not launching a DE (Desktop Environment) on startup.. either headless your just have your pi set to not launch the Desktop.

1st... install the multiplexer

Code: Select all

sudo apt-get update && sudo apt install tmux


create a new file in the home directory " ~/.tmux.conf "
contents of the file

Code: Select all

set -g status off
This will remove the default status bar and the session will appear as a normal console. (unless you want to see the default green status bar to remember its a tmux session... skip the suggested conf setting)

Now you want to setup .bashrc to automatically launch a named tmux session when starting up and the login as completed.

Edit ~/.bashrc and add the following to the bottom of the file

Code: Select all

if ! { [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; } then
	[[ $(/usr/bin/tty) == "/dev/tty1" ]] && exec /usr/bin/tmux new -s main
fi
This will test if .bashrc is being sourced within a multiplex window or not (using screen or tmux)... we want to look for when its "NOT", and then test if .bashrc is being sourced from the default tty1 ( this means the default boot-up console, or what would be displayed on a monitor hooked up to the pi). This will prevent executing the statement when you ssh to the pi or open a terminal from your DE etc...

Now... in this context where we want a single tmux window which automatically launches a script within that window when its created... aka the .bashrc is sourced... lets add another 3 lines of code to the .bashrc to test if we "ARE" in a tmux session and if so.. execute some script or program etc... in this example I am going to reference an example whiptail script. You could elif the previous statement if you wanted... 6 eggs half a dozen... etc... but I like keeping it separate so I can block comment without having an empty elif that might complain.

Create a folder named test or something in the home directory.. example ~/test

edit ~/.bashrc and add the following to the bottom of the file

Code: Select all

if { [ "$TERM" = "screen" ] && [ -n "$TMUX" ]; } then
	sh ~/test/menu.sh
fi
Now create a new file in ~/test ... ~/test/menu.sh

Code: Select all

#! /bin/bash

clear

while [ 1 ]
do
CHOICE=$(
whiptail --title "Programs to Execute" --menu "Make your choice" 16 100 9 \
	"1)" "Script A that does something" \
	"2)" "Script B that does something" \
	"3)" "Script C that does something" \
	"4)" "Exit Menu"  3>&2 2>&1 1>&3	
)

case $CHOICE in
	"1)") 
		sh menuselection.sh 
	;;
	"2)") 
		sh menuselection.sh  
	;;
	"3)") 
		sh menuselection.sh  
	;;
	"4)") exit 
	;;
esac
whiptail --msgbox "$CHOICE completed" 20 78
done
exit

Create a new file in ~/test ... ~/test/menuselection.sh

Code: Select all

echo "doing nothing for a couple seconds...."
sleep 5s
echo "completed"
At this point the example is all set.... if you rebooted the pi... and its set to auto-login and not launch X (go into a desktop environment).. then on the main monitor you should see a whiptail menu.

without rebooting... you can simply execute the command

Code: Select all

tmux new -s main
From another computer..... you can ssh to the pi and then execute the following

Code: Select all

tmux a -t main
This is launching tmux and attaching to session named "main". In your terminal window from which you made your ssh connection ... you will now see what is displayed on the pi's tty1 console.... kind of like a VNC session but for the command line.

To "Detach" from the session... you press the following keyboard keys " <Ctrl> + <b> " then release and then press the <d> key..
Google tmux cheat sheet or just tmux too read up on how to use it... but in this context.. the attach and how to detach I mentioned above is all you need to know

So now the last part... How do I wire up buttons on GPIOs to act as keyboard key presses.... well you can do it the slightly complicated way or just utilize what tmux can do for you.

So you may have been pondering why utilize tmux if I don't care about remote session control as in seeing what the screen is showing perspective... well... tmux remote session control is just that.. you can remotely control the session down to the key presses executed within any window within any session of tmux.

in this example, the session name is "main" and there is only one window.. "0" for zero based array.

With the tmux session running and the whiptail example running.... from another computer, ssh to the pi... and execute the following command and watch what happens on the display the pi is hooked up to

Code: Select all

tmux send-keys -t main.0 DOWN
then

Code: Select all

tmux send-keys -t main.0 ENTER
So now you can write a script ... python, bash etc.. with a loop monitoring your GPIO pins with buttons... and on desired state change... execute a shell command such as above... make that script a service so that its executed and running on the pi startup.

You can utilize the send-keys command for tmux to relaunch any script within the session window etc... the possibilities are limited to your imagination.

User avatar
liudr
Posts: 672
Joined: Sat Jun 01, 2013 12:11 am
Location: Central MN, USA
Contact: Website

Re: Load same history every time

Wed Oct 10, 2018 2:05 pm

tpyo kingg wrote:
Mon Oct 08, 2018 2:54 pm
That would go in the user's .bashrc file.
Thanks.
Arduino data loggers, user interface, printed circuit board designer since 2009, RPI 3B 2B 2B Zero Jessie, assembly/C/C++/java/python programmer since the 80's

Return to “General discussion”