简体   繁体   中英

How write history in a file with a shell script

i do a long manipulation, so i want to take my history and put in a file "text.txt" then i rm the history, but it doesn't work i dont know why.

#!/bin/bash


while true
do
     history -r
     history >> root/history.txt
     echo "history.txt rempli" &
     echo "test automatise" >> history.txt
     rm history
    sleep 10
done

it only write "test automatise", history is orange but bash tells me than he can't rm history because he doesn't know it.

I do./test.sh & when i want to start the scrpt.

Thanks !!

rm history will try to delete history file or directory, it probably does not exist and will throw an error, instead clear your history with history -c

history does not work because it is not ran in the current shell script, you can make it work with source :

#!/bin/bash


while true
do
     history -r
     history >> root/history.txt
     echo "history.txt rempli" &
     echo "test automatise" >> history.txt
     history -c
    sleep 10
done

and then run

$ source test.sh &

learn more about source: https://superuser.com/questions/46139/what-does-source-do

learn more about why history does not work without source: https://unix.stackexchange.com/questions/5684/history-command-inside-bash-script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM