User Tools

Site Tools


shell_programming

Shell Programming

sed

$ sed “s/day/night/” <old >new
$ sed “s/day/night/; 14 q” <old >new    # chain commands, quit after line 14
$ sed -E "s/\{s:([0-9]*)[[:space:],]*l:([0-9]*)[[:space:] ,]*n:([0-9]*).*/\1\t\2\t\3/g" <test.js

awk

Run program in background

$ tar -czf home.tar.gz . &  # append & to run command in background

or

$ tar -czf home.tar.gz .
Ctrl+Z      # stop the job
$ bg   # then continue the job in the background
$ jobs # list jobs running in background
$ fg  # put a background job back into the foreground
$ disown -h %1  # separate job from shell 
$ nohup tar -czf home.tar.gz . &  # run in background and separate it from shell
$ firefox </dev/null &>/dev/null &
$ kill %2  # kill job number 2
$ fg %2  # put job 2 in the foreground
$ bg %2  # put job 2 in the background 
$ export PS1='$ '    # set prompt
$ . activate # dot command, run in parent context from within script
$ source activate # same as dot command

Directory Stack

$ pushd $PWD # push current directory onto the stack
$ cd /home/whatever
$ popd # return to pushed directory

Redirection

$ cat >newfile.txt                 # redirect stdout, replace file
$ cat >>newfile.txt                # redirect stdout, append to file
$ cat &newfile.txt                 # redirect stderr, replace file
$ cat &>newfile.txt                # redirect stdout and stderr, replace file
$ cat &>>newfile.txt               # redirect stdout and stderr, append to file
$ cat <myfile.txt                  # redirect stdin
$ cat <oldfile.txt >newfile.txt    # copy a file

Pipe

$ ps -e | grep ping                   # redirect stdout of first command to stdin of second command
$ ps -e | grep ping | less            # chain pipes

chmod

$ chmod +x ~/.local/bin/myscript.sh   # make a script executable

chown

$ chown root myfile.txt    # change owner of myfile to root

How to stop a program

Signaling

Kill

How to start a program

$ <command>
$ bash <command>
$ source <command>
$ . <command>
$ exec <command>
$ <command> &
$ nohup <command>
$ systemd
$ nice <command>

foreground, background, daemon

run in background run as background job redirect stdout and stderr

$ disown %1    # suppress SIGHUP and remove from jobs list
$ disown -h %1 # suppress SIGHUP 
$ nohup <command> # suppress SIGHUP when starting command

glossary

  • session
  • state Ss
  • terminal session
  • one foreground process
  • multiple background processes
  • multiple background jobs
shell_programming.txt · Last modified: 2021/01/28 05:46 by 127.0.0.1

Except where otherwise noted, content on this wiki is licensed under the following license: Public Domain
Public Domain Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki