.bin/check_installed.sh:fc:3: no such event: 1 when trying to run a set of commands from script that work fine when ran in the terminal directly

3 years ago by promitheas to c/shell

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 1 point 3 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.

path: 0 1758697, hotness: undefined, score: 1, children: 3
promitheas 1 point 3 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

path: 0 1758697 1758865, hotness: undefined, score: 1, children: 2
pnutzh4x0r 2 points 3 years ago

An alternative to making a shell script is to make an alias or a function instead. That way, it runs in your current shell session and you can access the history command.

Additionally, you could always dump the output of the history command outside the shell script and then run the shell script on that file after you have dumped it.

path: 0 1758697 1758865 1759524, hotness: undefined, score: 2, children: 1
promitheas 2 points 3 years ago

I managed to get it working with an alias, not sure how it didn't come to me before! I am however still curious if it is possible to achieve the same result through a script, and if so how that would be done, so if its alright I'll leave this unmarked as solved for a short while longer in the hope a solution is given by someone. Thank you!

path: 0 1758697 1758865 1759524 1761167, hotness: undefined, score: 2, children: 0
narshee 1 point 3 years ago

I don't know zsh, but the curly brackets like that are not correct in posix sh. { commands; } is correct. { history | grep -e 'pacman -S\s' -e 'pacman -R\s' -e 'yay -S\s' -e 'yay -R\s' | tail -n 50; } > ~/history_installed I think that fixes it. Also check out shellcheck if you don't have already.

path: 0 1758969, hotness: undefined, score: 1, children: 0
shell
shell

@programming.dev

login for more options
1615
50
1

From Ash, Bash and Csh to Xonsh, Ysh and Zsh; all shell languages are welcome here!

Rules:
  1. Follow Lemmy rules!
  2. Posts must relate to shell scripting. (See bottom of sidebar for more information.)
  3. Only make helpful replies to questions. This is not the place for low effort joke answers.
  4. No discussion about piracy or hacking.
  5. If you find a solution to your problem by other means, please take your time to write down the steps you used to solve your problem in the original post. You can potentially help others having the same problem!
  6. These rules will change as the community grows.

Keep posts about shell scripting! Here are some guidelines to help:

  • Allowed: Release Announcement of a command-line program designed for scripted use (e.g. bash, bats, awk, jq, coreutils/moreutils)
  • Allowed: Tutorials on using shell languages or supplementary tools designed for scripted use
  • Allowed: Code review/help requests for shell languages or supplementary tools designed for scripted use
  • NOT Allowed: Announcement of a CLI or TUI program that is not designed for scripted use (Yes, your fancy Zsh plugin which pretty-prints the date and time using only builtins is very cool, but unless you actually want to discuss the code itself, please check out !commandline instead!)
  • NOT Allowed: Domain-specific tutorials that do not involve shell scripting as a core component (e.g. docker-compose, ansible, nix). If you really love one of these, I'm sure there's a community out there ready to talk about them!
  • NOT Allowed: Code review requests for non-shell programming languages and configuration languages (e.g. Python, Yaml)

In general, if your submission text is primarily shell code, then it is welcome here!

go to feed...