Shell Scripting

1333 readers
1 users here now

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:


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

founded 1 year ago
MODERATORS
26
27
 
 

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!

28
 
 

cross-posted from: https://lemmy.world/post/1838642

link-batch is a minimalist script that generate symlinks from a list in a text file. Usage :

link-batch.zsh link-list.txt

where link-list.txt contains two columns : the first one for the links and the second one for the targets. Example :

~/.config/kitty	~/myfiles/config/kitty
~/.config/nvim	~/myfiles/config/neovim
~/.config/MuseScore	~/myfiles/config/MuseScore/$HOST
...

The two columns must be separated by a tab.

Shell vars like $HOME or $HOST are evaluated to their values.

Can be used to quickly deploy all home links in a fresh box.

29
 
 

This is working just fine, but it's not finished. However I am not interested in working on it anymore.

30
 
 

I'm sure some of you have absolute monstrosities of sigils (I know I do, in my .zshrc alone). Post them without context, and try and guess what other users's lines are. If you want to provide context or guess, use the markdown editor to spoiler-tag your guesses and explanations!

31
32
 
 

i'm pretty new to the shell scripting world and not sure, if i should give my scripts a .sh or .bash extension.

not sure what the pros and cons are.

33
 
 

I came across this pure sh bible when browsing HN. Now I’d like to know if there is any introduction to pure “sh” scripting. It would be nice if it’s something downloadable as epub.

34
 
 

This came out of playing around with curl and the Lemmy API. I wanted a repeatable way to declare a bunch of shell parameters from a json object, so I wrote a jq module! Take a look at the README for examples, or once you install, run

source <( curl 'https://programming.dev/api/v3/community?id=267' | jq -r 'import "shell" as shell; .community_view | shell::param' )

Currently this project only provides a param function, and only supports Bash and Zsh. If I think of other useful helper functions, I may add them to the project.

This uses _ as a default prefix for each parameter. This is so things like {"PATH": "..."} generate typeset _PATH='...' and don't mangle your $PATH, for example. The prefix can be customized by passing in a string: param("mypfx").

Before declaring a 1.0, I'm planning on expanding shell support, and have the function determine how to output using the the $SHELL parameter:

# This does not work (yet!)
eval (curl $URL | jq -r '
  import "shell" as shell;
  "fish" as $SHELL |
  .community_view | param
')

Always up for feedback!

35
 
 

I found this post really helpful to anyone who is using tmux

36
 
 

YSH, or the shell formally known as oil, is touted as a possible upgrade path from Bash.

This is the first in a three-part series of posts re-introducing the language.

  1. Reviewing YSH
  2. Sketches of YSH Features (you are here)
  3. YSH, Narrow Waists, and Perlis-Thompson Problems (Not yet released)
37
1
[ysh] (1/3) Reviewing YSH (www.oilshell.org)
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/[email protected]
 
 

YSH, or the shell formally known as oil, is touted as a possible upgrade path from Bash.

This is the first in a three-part series of posts re-introducing the language.

  1. Reviewing YSH (you are here)
  2. Sketches of YSH Features
  3. YSH, Narrow Waists, and Perlis-Thompson Problems (Not yet released)
38
39
 
 

Feedback is still welcome, either here or as Merge Requests to my Gitlab Pages repo.