Alex's Slip-box

These are my org-mode notes in sort of Zettelkasten style

Shell tips

:ID: 3453ED9D-38E6-4EDA-9652-189BCABA429F

Just some various tips for writing shell scripts or doing cmd line stuff.

# Get the first n lines from STDOUT

We can use the head program for this. head will return the first n lines of a file, but can be used with STDOUT as well.

Example: get the docker image ID for the last created image.

docker images --format "{{.ID}}" | head -n 1

See also man head

# Forward arguments

Forward all arguments passed to a script to some other program called by the script.

$@ Represents all arguments, so in a script that does something with npm, for example, we can forward all the arguments passed to the script to npm:

npm $@

# Default arguments

If a parameter is unset or null, use a default value.

See also

npm run test ${FILE_PATH:-'tests/unit/**/*.spec.*(ts|js)'}

We can also set the parameter by using = instead of - above.

# List open files

Use lsof

It will include open sockets since anything IO is considered a file on a UNIX OS.

# Examples

# List Internet files

lsof -i 

…on a specific port

lsof -i :3000 

…or list all processes that are listening on some port

lsof -i -P -n | grep LISTEN

# Test if process is running

Here I wanted to test if reaper was already running before trying to start it. I don’t want two instances of the program. Note the & sends it to a subshell (child process)

if ! [[ "`pidof -x reaper -o %PPID`" ]];
  then reaper &

Search Results