Shell scripts and tools.
1 User Commands 2 System Calls 3 C Library Functions 4 Devices and Special Files 5 File Formats and Conventions 6 Games et. Al. 7 Miscellanea 8 System Administration tools and Deamons
-b filename block special file -c filename special character file -d dirname check for directory existence -e filename check for file existence -f filename check for regular file existence not a directory -G filename check if file exists and is owned by effective group ID. -g filename true if file exists and is set-group-id. -k filename sticky bit -L filename symbolic link -O filename true if file exists and is owned by the effective user id. -r filename check if file is a readable -S filename check if file is socket -s filename check if file is nonzero size -u filename check if file set-user-id bit is set -w filename check if file is writable -x filename check if file is executable -z <string> ... true if the length of the string is non-zero
example
#!/bin/bash file=./file if [ ! -e "$file" ]; then echo "File does not exist" else echo "File exists" fi
$ cat file.txt | tee >(pbcopy) >(do_stuff) >(do_more_stuff) | grep errors
$ ag -l <pattern> | xargs sed -i '' -E 's/<old>/<new>/g'
$ cat file.txt | sed -e '1,2d'
$ cat file.txt | awk '{$3=$1; gsub(/0[12345]_/, "", $3); $2="|"}{print}'
$ screen_width=${COLUMNS:-$(tput cols)}ssh -i <path/to/file> <name>@<ip>
or with a ~/.ssh/configfile
ssh <Host>
lsof -u <ownername>
Named pipes are cool to create background processes with that can be addressed
by name to do stuff with. It creates a physical file on the system that can be
used from any shell process. The code below passes the output of pipe to
cat, which then writes to out.txt. When passing in a command to pip
(ls -la in this case), it pops back out at the other side.
$ mkfifo pipe $ cat < pipe > output.ext $ ls -la > pipe
$ tail -r <file>
npm ex <module name> <command> ... execute a command in the module dir
DNS lookup utility
# example
$ dig @127.0.0.1 -p 5000 something.foo +short
1.1.1.1Vim like file manager. Useful to do bulk directory operations. Offers different views on files.
System / gfx configuration tool.
boot manager
list all pci devices
List all available devices. Useful to determine how to partition.
Can be used to repair machines that have lost root access / are unbootable for
other reasons. Live cd's ftw! Also used to reset the pid of a tree of
processes, a commonly known technique used with docker.
$ playerctl
# bash $ <command> 2>&1 /dev/null # POSIX sh $ <command> >/dev/null 2>&1
cat << EOF oh my, such nice text EOF
if [ "$_" = "$0" ] then echo 'yup, script is directly called' else echo 'nope, script is not directly called' fi
case $1 in "") usage; exit 1 ;; -h|--help) usage; exit ;; -l|--link) link "$@" ;; *) readonly name=$1 ;; esac
$ fmt -80
$ echo $RANDOM
In shell it's not recommended to use expr or bc, instead use:
foo=5
res=$((foo - 2 / 3))printf "%02d" 4 # => 04
Cleaning up after yourself is essential in programming. The shell trap
command listens to a POSIX signal, and then runs a command. It's event-driven
programming in Shell!
warning: trap cannot process SIGKILL, as is the nature of the signal.
Use kill -9 as a last resort.
Here's an example of trap in action:
readonly TMP_FILE='/tmp/bar.file' # delete the file if the program is prematurely ended trap "rm $TMP_FILE; exit" SIGHUP SIGINT SIGTERM pr $1 > "$TMP_FILE"
Renaming is made easy using the rename(1) command. Just plop in a regex and
the renaming is done for you:
# Change foo to bar in matching filenames $ rename 's/foo/bar/' *.txt # Convert to lower case $ rename -c *.txt # Replace whitespace with underscores $ rename 's/\s+/_/g' *.txt # No action, just show what renames would occur $ rename -n 's/foo/bar/' *.txt
The init process is the heart of the OS. It schedules everything that happens on top of the kernel.