[oh@localhost shell]$ ./s1.sh
bash: ./s1.sh: Permission denied
[oh@localhost shell]$ echo $PATH
/usr/lib/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/oh/bin
[oh@localhost shell]$
[oh@localhost shell]$ ls -l
total 4
-rw-rw-r--. 1 oh oh 74 Mar 25 14:12 s1.sh
[oh@localhost shell]$ chmod u+x s1.sh
[oh@localhost shell]$ ./s1.sh
Sun Mar 25 14:15:52 CST 2018
oh tty1 2018-03-25 21:41 (:0)
oh pts/0 2018-03-25 21:43 (:0.0)
oh pts/1 2018-03-25 13:59 (:0.0)
[oh@localhost shell]$ cat s1.sh
#!/bin/bash
#this script displays the date and who's logged on
date
who
[oh@localhost shell]$
-----------
Display all 238 possibilities? (y or n)
[oh@localhost shell]$ s
bash: s: command not found
[oh@localhost shell]$
[oh@localhost shell]$ s1.sh
bash: s1.sh: command not found
[oh@localhost shell]$ ls -;
ls: cannot access -: No such file or directory
[oh@localhost shell]$ ls -l
total 4
-rwxrw-r--. 1 oh oh 74 Mar 25 14:12 s1.sh
[oh@localhost shell]$ s1.sh
bash: s1.sh: command not found
[oh@localhost shell]$ ln -s s1.sh ~/bin
[oh@localhost shell]$ ls -l ~/bin
total 8
-rwxrw-r--. 1 oh oh 438 Mar 17 15:00 md
lrwxrwxrwx. 1 oh oh 5 Mar 25 14:18 s1.sh -> s1.sh
-rwxrw-r--. 1 oh oh 121 Mar 21 17:34 vps
[oh@localhost shell]$ s1.sh
bash: s1.sh: command not found
[oh@localhost shell]$ s1.sh -> s1.sh
bash: s1.sh: command not found
[oh@localhost shell]$
--------------------
[oh@localhost shell]$ ./s1.sh
./s1.sh: line 3: unexpected EOF while looking for matching `"'
./s1.sh: line 6: syntax error: unexpected end of file
[oh@localhost shell]$ cat s1.sh
#/bin/bash
echo i'll help you
echo "i'll help you"
echo 'i'll help you'
echo i\'ll help you
[oh@localhost shell]$ cat -n s1.sh
1 #/bin/bash
2 echo i'll help you
3 echo "i'll help you"
4 echo 'i'll help you'
5 echo i\'ll help you
[oh@localhost shell]$ echo i'll help you
> ;
>
> '
ill help you
;
[oh@localhost shell]$ echo "i'll help you"
i'll help you
[oh@localhost shell]$ echo i\'ll help you
i'll help you
[oh@localhost shell]$
[oh@localhost shell]$ vim ~/bin/cls
[oh@localhost shell]$ chmod u+x ~/bin/cls
[oh@localhost shell]$ cat ~/bin/cls
#!/bin/bash
#this will clear all on vt terminal
printf "\033c"
[oh@localhost shell]$
[oh@localhost shell]$ ls
s1.sh
[oh@localhost shell]$ vim s1.sh
[oh@localhost shell]$ ./s1.sh
ill help you
i'll help you
ill help you
i'll help you
[oh@localhost shell]$ cat s1.sh
#!/bin/bash
echo i'll help you'
echo "i'll help you"
echo 'i'll help you
echo i\'ll help you
[oh@localhost shell]$
环境变量:
shell维护着一组环境变量,用来记录特定的系统信息。比如系统的名称,登录到系统上的用户的名称,用户的系统ID(UID),用户的默认主目录以及shell查找程序的搜索路径。 使用set:显示完整的,活动的环境变量列表:
[oh@localhost shell]$ set
BASH=/bin/bash
BASHOPTS=checkwinsize:cmdhist:expand_aliases:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
COLORS=/etc/DIR_COLORS
COLORTERM=gnome-terminal
COLUMNS=96
CVS_RSH=ssh
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-w849Z2uAAy,guid=439305c8816b13b63891df1e00000033
DESKTOP_SESSION=gnome
DIRSTACK=()
DISPLAY=:0.0
EUID=500
GDMSESSION=gnome
GDM_KEYBOARD_LAYOUT=us
GDM_LANG=en_GB.UTF-8
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_KEYRING_PID=2820
GNOME_KEYRING_SOCKET=/tmp/keyring-xwXL4Z/socket
GROUPS=()
GTK_RC_FILES=/etc/gtk/gtkrc:/home/oh/.gtkrc-1.2-gnome2
G_BROKEN_FILENAMES=1
HISTCONTROL=ignoredups
HISTFILE=/home/oh/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/home/oh
HOSTNAME=localhost.localdomain
HOSTTYPE=i386
HTTP_PROXY=http://127.0.0.1:1080/
ID=500
IFS=$' \t\n'
IMSETTINGS_INTEGRATE_DESKTOP=yes
IMSETTINGS_MODULE=IBus
LANG=en_GB.UTF-8
LESSOPEN='||/usr/bin/lesspipe.sh %s'
LINES=23
LOGNAME=oh
LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.tbz=01;31:*.tbz2=01;31:*.bz=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:'
MACHTYPE=i386-redhat-linux-gnu
MAIL=/var/spool/mail/oh
MAILCHECK=60
OLDPWD=/home/oh/linux/git/manlist
OPTERR=1
OPTIND=1
ORBIT_SOCKETDIR=/tmp/orbit-oh
OSTYPE=linux-gnu
PATH=/usr/lib/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/oh/bin
PIPESTATUS=([0]="0")
PPID=3446
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/home/oh/linux/git/manlist/shell
QTDIR=/usr/lib/qt-3.3
QTINC=/usr/lib/qt-3.3/include
QTLIB=/usr/lib/qt-3.3/lib
QT_IM_MODULE=ibus
SESSION_MANAGER=local/unix:@/tmp/.ICE-unix/2830,unix/unix:/tmp/.ICE-unix/2830
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=2
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
SSH_AUTH_SOCK=/tmp/keyring-xwXL4Z/socket.ssh
TERM=xterm
UID=500
USER=oh
USERNAME=oh
WINDOWID=69208457
WINDOWPATH=1
XAUTHORITY=/var/run/gdm/auth-for-oh-BMVXbU/database
XDG_SESSION_COOKIE=b1d24587a88de5a033e32b1000000014-1521985318.25884-2011103072
XMODIFIERS=@im=ibus
_=cls
colors=/etc/DIR_COLORS
http_proxy=http://127.0.0.1:1080/
__udisks ()
{
local IFS='
';
local cur="${COMP_WORDS[COMP_CWORD]}";
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--show-info" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--inhibit-polling" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--mount" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--unmount" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--detach" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-refresh" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--ata-smart-simulate" ]; then
_filedir || return 0;
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--set-spindown" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
if [ "${COMP_WORDS[$(($COMP_CWORD - 1))]}" = "--poll-for-media" ]; then
COMPREPLY=($(compgen -W "$(udisks --enumerate-device-files)" -- $cur));
else
COMPREPLY=($(IFS=: compgen -W "--dump:--inhibit-polling:--inhibit-all-polling:--enumerate:--enumerate-device-files:--monitor:--monitor-detail:--show-info:--help:--mount:--mount-fstype:--mount-options:--unmount:--unmount-options:--detach:--detach-options:--ata-smart-refresh:--ata-smart-wakeup:--ata-smart-simulate:--set-spindown:--set-spindown-all:--spindown-timeout:--poll-for-media" -- $cur));
fi;
fi;
fi;
fi;
fi;
fi;
fi;
fi;
fi
}
[oh@localhost shell]$
在脚本中使用环境变量:加个$符号。
eg。。
在为了表示$符号,则要转义:
[oh@localhost shell]$ ./s2
User info for userid:oh
UID:500
HOME:/home/oh
[oh@localhost shell]$ cat s2
#!/bin/bash
#display user information form the system.
echo "User info for userid:$USER"
echo "UID:$UID"
echo HOME:$HOME
[oh@localhost shell]$ echo "now $12 is 12"
now 2 is 12
[oh@localhost shell]$ echo
[oh@localhost shell]$ echo "now \$12 is 12"
now $12 is 12
[oh@localhost shell]$
[oh@localhost shell]$ ./s2
User info for userid:oh
UID:500
HOME:/home/oh
[oh@localhost shell]$ cat s2
#!/bin/bash
#display user information form the system.
echo "User info for userid:$USER"
echo "UID:$UID"
echo HOME:$HOME
------
shell 脚本会自动决定变量类型,所以直接 var=1
var2=“string"
在shell脚本完成时删除变量。
那么时候要在变量前加美元符号呢??
使用时要加$,赋值时不用
[oh@localhost shell]$ cat s3
#!/bin/bash
#assigning a variable value to another variable
value1=10
value2=$value1
value3=value2
[oh@localhost shell]$ ./s3
[oh@localhost shell]$ vim s3
[oh@localhost shell]$ ./s3
value3=value2 value2=10 value1=10
--------------
反引号:
`
把命令的输出赋给变量:
例如:
o=`date`
两个:`圈了起来
常用在:日志文件名抓取日期信息:
[oh@localhost shell]$ date +%y
18
[oh@localhost shell]$ date +%y%m%d
180325
[oh@localhost shell]$ cp s s4
[oh@localhost shell]$ ls
s s1.sh s2 s3 s4
[oh@localhost shell]$ vim s4
[oh@localhost shell]$ ./s4
[oh@localhost shell]$ ls
log.180325 s s1.sh s2 s3 s4
[oh@localhost shell]$ cat log.180325 |less
[oh@localhost shell]$ cat s4
#!/bin/bash
#copy the /usr/bin directory listing to a log file
today=`date +%y%m%d`
ls /usr/bin -al >log.$today
[oh@localhost shell]$
好像还是不怎么样!!!
wc命令:
NAME
wc - print newline, word, and byte counts for each file
文本的行数,单词数 及字节数
[oh@localhost shell]$ ls
log.180325 s s1.sh s2 s3 s4
[oh@localhost shell]$ wc <log.180325
1587 14656 99305
[oh@localhost shell]$
[oh@localhost shell]$ cat -n log.180325 |less
[oh@localhost shell]$
真的有1587行
内联输入重定向:inline input redirection ,允许你在命令运行而不是在文件指定输入重定向的数据。符号是双小于号<<
还得指定一个文本标记来划分要输入的数据的开始和结尾。可以是任何字符。
[oh@localhost shell]$ wc <<err
> lo
> cc
> c vkjflkfjahjjjd
> djdkfj
> ddfjkdfj
> err
5 6 39
[oh@localhost shell]$ wc <<EOF
> ok
> bye bye
> i'm ok
> EOF
3 5 18
------
管道:|
使用:管道链接 piping
---------------
执行数学运算:
两种途径 expr 命令
1》expr 命令
有()代表优先级
特别的操作符:
string : regexp
match string regexp
substr string pos length
index string chars
length string
+ token
(expressin)
来自:https://blog.csdn.net/adcxf/article/details/3001275--------------------------------》》》》》
expr命令是一个手工命令行计数器,用于在UNIX/LINUX下求表达式变量的值,一般用于整数值,也可用于字符串。
–格式为:
expr Expression(命令读入Expression 参数,计算它的值,然后将结果写入到标准输出)
–参数应用规则:
用空格隔开每个项;
用 / (反斜杠) 放在 shell 特定的字符前面;
对包含空格和其他特殊字符的字符串要用引号括起来
–expr用法实例讲解:
(1)、计算字串长度
> expr length “this is a test”
14
(2)、抓取字串
> expr substr “this is a test” 3 5
is is
(3)、抓取第一个字符数字串出现的位置
> expr index “sarasara” a
2
(4)、字串真实重现
> expr quote sara
sara
(5)、整数运算
> expr 14 % 9
5
> expr 10 + 10
20
> expr 1000 + 900
1900
> expr 30 / 3 / 2
5
> expr 30 /* 3 (使用乘号时,必须用反斜线屏蔽其特定含义。因为shell可能会误解显示星号的意义)
90
> expr 30 * 3
expr: Syntax error
(6)、增量计数
说明:expr在循环中用于增量计算。先将变量初始化为0,然后循环值加1,反引号的用法为命令替代。
> LOOP=0
> LOOP=`expr $LOOP + 1`
(7)、数值测试
说明:用expr测试一个数。如果试图计算非整数,则会返回错误。
> rr=3.4
> expr $rr + 1
expr: non-numeric argument
> rr=5
> expr $rr + 1
6
(8)、模式匹配
说明:expr也有模式匹配功能。可以使用expr通过指定冒号选项计算字符串中字符数。.*意即任何字符重复0次或多次。
> VALUE=account.doc
> expr $VALUE : ‘.*’
8
在expr中可以使用字符串匹配操作,这里使用模式抽取.doc文件附属名。
$expr $VALUE : ‘/(.*/).doc’
accounts
1+2 与 1 + 2
要用空格--------------
[oh@localhost shell]$ expr 1+4
1+4
[oh@localhost shell]$ expr 3*4
3*4
[oh@localhost shell]$ expr 1/3
1/3
[oh@localhost shell]$ expr 1%3
1%3
[oh@localhost shell]$ expr 1
1
[oh@localhost shell]$ expr 1 % 3
1
[oh@localhost shell]$ expr 3 * 4
expr: syntax error
[oh@localhost shell]$ expr 2*2
2*2
[oh@localhost shell]$ expr 2 * 2
expr: syntax error
[oh@localhost shell]$ expr 2 \* 3
6
[oh@localhost shell]$
*号在shell中特别的意思,所以要转义
而在脚本中要用反引号 `expr 3 + 4`
所以引出 $[1 + 5]
[oh@localhost shell]$ ./s3
value3=3 value2=10 value1=10
[oh@localhost shell]$ cat s3
#!/bin/bash
#assigning a variable value to another variable
value1=10
value2=$value1
value3=value2
value3=$[1 + 2]
echo value3=$value3 value2=$value2 value1=$value1
注意空格:
[oh@localhost shell]$ ./s3
value3=3 value2=[2+2] value1=10
[oh@localhost shell]$ cat s3
#!/bin/bash
#assigning a variable value to another variable
value1=10
value2=$value1
value3=value2
value3=$[1 + 2]
value2=[2+2]
echo value3=$value3 value2=$value2 value1=$value1
虽然只支持整数。但是不用担心*号了
但可以用 z shell
--------------
解决浮点计算问题:
最常用的:bc计算器
在bc中
能识别:
数字
变量
注释 # 及 /* */
表达式
编程语句
函数
bc
quit退出
[oh@localhost shell]$ bc
bc 1.06.95
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
1+3
4
quit
[oh@localhost shell]$
浮点运算由内建的一个叫scale 的变量控制,scale=3,则小数有三位
scale默认是0,当然你可定义变量,并且使用print var 打印
----
在脚步中使用bc:
首先的方式是:使用`,反引号
var= `echo "scale = 4;3.44 / 5" | bc `
var=`echo "options;exprssion"|bc`
[oh@localhost shell]$ ./s5
10
.750
[oh@localhost shell]$ cat s5
#!/bin/bash
#use bc to solve it
v1=23
v2=33
var2=`echo "scale =3;$v2 - $v1" |bc `
echo $var2
var1=`echo "scale=3;3 / 4" |bc`
echo $var1
[oh@localhost shell]$
你可以考虑使用输入重定向到bc,但是必须写在额外的文件里
于是考虑内联输入重定向:
var=`bc << EOF
scale = 4
var1=2
var2=3
var1 + var2
EOF
`
但是变量是有作用域的,在bash里bc定义的变量bc外不能用
------------
shell中运行的每个命令都使用 退出状态码 exit status 来告诉shell它完成了处理。推出状态码是 0~255中的数字。
查看退出状态码: $? 来查看最近一个命令的退出状态码
[oh@localhost shell]$ $?
bash: 0: command not found
[oh@localhost shell]$ echo $?
127
[oh@localhost shell]$ $?
bash: 0: command not found
[oh@localhost shell]$ echo $
$
[oh@localhost shell]$ echo $?
0
[oh@localhost shell]$
状态码:
0 命令成功结束
1 通用未知错误
2 误用shell命令
126 命令不可执行
127 没找到命令
128 无效退出参数
128+x linux 信号x的严重错误
130 命令别 ctrl + c终止了
255 退出状态码越界
---------
于是可以通过退出状态码来确定命令是否成功执行
在脚本中,最后一个命令的退出状态码作为整个脚本的退出状态码
你可使用exit 5
来指定exit status 为 5
或者使用 exit $var
但是要注意值大于255的话,会取对255余数