Hello everyone. I wrote this command in the terminal directly and got the desired and expected output - that being the last 50 occurrences of me installing or removing a package with pacman or yay: history | grep -e 'pacman -S\s' -e 'pacman -R\s' -e 'yay -S\s' -e 'yay -R\s' | tail -n 50 > ~/history_installed

I now want to make this runnable as a script for obvious reasons, but when add it to a script and run it I get the following error: /home/user/.bin/check_installed.sh:fc:3: no such event: 1

Here is my entire script:

#!/bin/zsh

{history | grep -e 'pacman -S\s' -e 'pacman -R\s' -e 'yay -S\s' -e 'yay -R\s' | tail -n 50} > ~/history_installed

Note: /home/user/.bin is in my path. Verified by successfully running another script in there from a different location.

Please help me figure this out if you could. I am running zsh with oh-my-zsh. Thanks in advance!

  • pnutzh4x0r@lemmy.world
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 years ago

    I think the issue is that history is a shell built-in and not an actual program (ie. external command) and it typically only works in an interactive shell session.

    A workaround could be to access the $HISTFILE directly:

    {cat $HISTFILE | grep ...
    

    Of course, you can use also just do:

    {grep -e ... $HISTFILE | ...}
    

    if you are opposed to the cat at the beginning.

    • promitheas@iusearchlinux.fyiOP
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 years ago

      I tried using cat but I got the same result. I must admit I wasn’t aware that history is a shell built in and not a program. Given that is the case, would it not be very difficult to get the contents of history int o a temporary file from a shell script as I am attempting to do? Here is the new line which I attempted:

      {cat $HISTFILE | grep -e 'pacman -S\s' -e 'pacman -R\s' -e 'yay -S\s' -e 'yay -R\s' | tail -n 50} > ~/history_installed

  • narshee@iusearchlinux.fyi
    link
    fedilink
    English
    arrow-up
    1
    ·
    edit-2
    2 years ago

    I don’t know zsh, but the curly brackets like that are not correct in posix sh. { commands; } is correct. I think that fixes it. Also check out shellcheck if you don’t have already.