vim ~/.bashrc
function h1(){
echo hello world
}
Source ~/.bashrc (. ~/.bashrc)
Then type h1 in the terminal.
source
?unset h1
open a file called h2 and edit it.
Vim ./h2
Write a script as follows.
#!/bin/bash
Echo hello world
echo $PATH
pwd
export Path=”$PWD:$PATH”
chmod +x h2
(or ./h2)
whereis h2 or just type which h2
type h2
Man echo
help echo
tldr echo
brought by shell buildin/shell itself
Offered by system
customized function
written executable scripts
name=$* or name=$@
then $@ will pass your parameters as separate parameters, whereas $* will just pass all params as a single parameter.
$0 Expands to the name of the shell or shell script.
variables
function or the variables of a script
env variables
$#
refer to The value of the total number of command line arguments passed.
date "+%Y-%m-%d %H:%M:%S"
echo $( date "+%Y-%m-%d %H:%M:%S")
grep judi name.list
cut -d' ' -f1 name.list
cut -d' ' -f2 name.list
-d : delimiter
-f: field
grep jdu name.list | cut -d' ' -f2
grep jdu name.list | cut -d' ' -f2-3
A pipeline is a sequence of one or more commands separated by one of the control operators | or |&.
TS=$(date "+%Y-%m-%d %H:%M:%S")
ID=$1
name=$(grep $ID name.list 2>/dev/null | cut -d' ' -f2)
if [[ "" == "$name" ]]
then
name="unkown"
fi
echo "[$TS] hello $name"
FYI:[[ or ]] has space before and after
>
file redirects stdout to file
1>
file redirects stdout to file
2>
file redirects stderr to file (Standard error)
&>
file redirects stdout and stderr to file
> file 2>&1
redirects stdout and stderr to file
/dev/null is the null device it takes any input you want and throws it away. It can be used to suppress any output.
Note that > file 2>&1 is an older syntax which still works, &> file is neater, but would not have worked on older systems.
Piping: a mechanism for sending data from one program to another
date >data.log
date >>data.log
date 2>data.log
Abc > data.log 2>&1
Abc >data.log 2>erroe.log
The former reads data from a file, the latter from standard input provided by the shell
Ps aux | grep ^root |less -N
Ps aux | grep ^root |cat -n
Ps aux | grep ^root |wc -l
Less -N (Add ids to each line)
History | head
History | tail
History | head -3
History | tail -3
history | sed -E 's/^ +//' | cut -d' ' -f3 | sort | uniq -c | sort -n
It means that the result of running the program is different.
exit (0): Run the program normally and exit the program;
exit (1): abnormal operation leads to exit the program;
Esc->:wq