Giter VIP home page Giter VIP logo

fsh's Introduction

Functional shell scripts | Spencer Tipping
Licensed under the terms of the MIT source code license

These scripts provide a functional shell environment. This gives you the ability
to use constructs similar to those in functional programming languages, but on
the shell level.

This project is uninspired at present and needs ideas.

The 'each' command takes each line of input and maps it to _ in the command you
give it. Iteration terminates if the command returns nonzero status.

ls | each echo _                                        # The same as ls
ls | each cat _                                         # Concatenates everything in this directory
ls | each 'ln -s _ _.txt'                               # Symlinks each file as file.txt
ls | each 'ln -s _ $(md5sum _ | awk "{print \$1}")'     # Symlinks each file as the MD5 of its contents
ls | each 'test -f _ && echo _ || :'                    # Only print regular files
ls | each 'echo _ $(($(wc -c < _) / $(wc -l < _)))'     # Display characters per line for each file

You can also use 'each' to emulate other concepts:

ls | each cat _                                         # This is really flat-mapping across files and lines of files
ls | each 'test -d _ && ls _ || echo _'                 # Flat-map one level across directories
ls | each 'test -d _ && ls _ | cat || echo _'           # Ditto, but this one gets proper output from ls

And you can use it with things besides ls:

ps -e | grep '\bdhclient\b' | cut -d' ' -f1 | each kill -9 _
ifconfig | grep '^\w' | sed 's/\s.*//' | each ifconfig _ down

'peach' is like 'each', but runs its jobs in parallel:

seq 1 10 | peach 'sleep 1 && echo _'                    # Returns after one second

The 'y' command repeats something until the command returns nonzero status. From
a functional point of view, it's a sort of fixed point on system state.

y ps -el | grep perl                                    # Print all Perl processes continuously (CPU hog)
y 'sleep 1 && ps -el' | grep perl                       # Print all Perl processes each second (not a CPU hog)
y 'sleep 1 && ps -el | grep perl | wc -l'               # Print the number of Perl processes each second
y 'test -f foo || touch foo'                            # Make sure the file 'foo' never goes away (CPU hog)
y y                                                     # No clue, but seems dangerous

'uncat' does the opposite of cat:

cat foo | command                                       # Prints the contents of foo into the command
command | uncat bar                                     # Writes the output of command into bar
command | uncat                                         # Writes the output of command into a tempfile and prints the tempfile

In general, uncat has the invariant that 'cat $(cmd | uncat)' is equivalent to 'cmd', modulo buffering.

'repeat' repeats something a fixed number of times:

repeat 10 echo hi                                       # Says 'hi' ten times

This is useful for things like benchmarking.

fsh's People

Contributors

spencertipping avatar nick-opusvl avatar

Stargazers

Ildar Shaimordanov avatar Devin Walters avatar  avatar Mike English avatar  avatar  avatar

Watchers

 avatar James Cloos avatar  avatar

Forkers

nmbooker

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.