zsh history: comment out dangerous commands: `#`

Если вы измените первую строку на

read number

вы получите поведение, которое вы ищете.

read var number

считывает два значения и сохраняет их в переменных с именами var и number. Если ввести только одно значение, seq 1 $number расширится до seq 1, что будет просто 1.

5
13.04.2017, 15:36
2 ответа

Конечно, используйте функцию ловушки zshaddhistory и отключите обычную обработку истории.

function zshaddhistory() {
  # defang naughty commands; the entire history entry is in $1
  if [[ $1 =~ "cp\ *|mv\ *|rm\ *|cat\ *\>|pv\ *|dd\ *" ]]; then
    1="# $1"
  fi
  # write to usual history location
  print -sr -- ${1%%$'\n'}
  # do not save the history line. if you have a chain of zshaddhistory
  # hook functions, this may be more complicated to manage, depending
  # on what those other hooks do (man zshall | less -p zshaddhistory)
  return 1
}

Таким образом, протестировано на zsh 5.0.8

% exec zsh
% echo good
good
% echo bad; rm /etc 
bad
rm: /etc: Operation not permitted
% history | tail -4
  299  exec zsh
  300  echo good
  301  # echo bad; rm /etc
  302  history | tail -4
%   

Похоже, это также работает с набором параметров extendedhistory .

4
20.08.2021, 11:58

Следующая функция основана на функции трига и исправляет histignorespace :

function zshaddhistory() {
  if [[ $1 =~ "^ " ]]; then
    return 0
  elif [[ $1 =~ "cp\ *|mv\ *|rm\ *|cat\ *\>|pv\ *|dd\ *" ]]; then
    1="# $1"
  fi
  # write to usual history location
  print -sr -- ${1%%$'\n'}
  # do not save the history line. if you have a chain of zshaddhistory
  # hook functions, this may be more complicated to manage, depending
  # on what those other hooks do (man zshall | less -p zshaddhistory)
  return 1
}
1
20.08.2021, 11:58

Теги

Похожие вопросы