shell 基本语法-1

4 篇文章 0 订阅
[root@cdh2 test1]# NAME=ZONE
[root@cdh2 test1]# 
[root@cdh2 test1]#  echo $NAME
ZONE
[root@cdh2 test1]# eval echo $NAME
ZONE
[root@cdh2 test1]# ll
total 28
-rw-r--r-- 1 root root  216 Jul 11 19:35 matchTest.scala
-rw-r--r-- 1 root root  697 Jul 11 06:30 prt1.class
-rw-r--r-- 1 root root 1066 Jul 11 06:30 prt1$.class
-rw-r--r-- 1 root root  586 Jul 11 06:29 prt1.scala
-rw-r--r-- 1 root root  594 Jul 11 06:26 prt.class
-rw-r--r-- 1 root root 1052 Jul 11 06:26 prt$.class
-rw-r--r-- 1 root root  120 Jul 11 06:19 prt.scala
[root@cdh2 test1]# vi test.txt
[root@cdh2 test1]# myfile="cat test.txt"
[root@cdh2 test1]# 
[root@cdh2 test1]# echo $myfile
cat test.txt
[root@cdh2 test1]# eval echo $myfile
cat test.txt
[root@cdh2 test1]# myfile="cat test.txt"
[root@cdh2 test1]# echo $myfile
cat test.txt
[root@cdh2 test1]# eval echo $myfile
cat test.txt
[root@cdh2 test1]# echo "Last argument is $(eval echo \$$#)"
Last argument is /bin/bash
[root@cdh2 test1]# 
[root@cdh2 test1]# echo "Last argument is $(eval echo $#)"
Last argument is 0
[root@cdh2 test1]# 
[root@cdh2 test1]# cd `dirname $0`
[root@cdh2 bin]# cd -
/user/local/test1
[root@cdh2 test1]# pwd
/user/local/test1
[root@cdh2 test1]# echo `dirname $0`
/bin
[root@cdh2 test1]# DAY=`date +%Y%m%d`
[root@cdh2 test1]# echo $DAY
20160711
[root@cdh2 test1]# DEL_DAY=`date -d -10days +%Y%m%d`
[root@cdh2 test1]# echo $DEL_DAY
20160701
[root@cdh2 test1]# DEL_DAY=`date -d -12days +%Y%m%d`
[root@cdh2 test1]# echo $DEL_DAY
20160629

  scala
  echo -e "ab\bc"
  echo -e "abc"
  echo -e "ab\bc"
  echo -e "a\tb\tc\nd\te\tf"
  echo "a\tb\tc\nd\te\tf"
  echo -e "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
  

  echo -e \
  "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
  
  echo -e "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
  echo -e \
  "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
  echo -e "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
  echo -e "\e[1;31m abcd \e[0m"
  echo -e "Mr. Shen Chao is the most honest man in LampBrother"
  pwd
  ll
  mkdir shell-test
  ll
  cd shell-test
  ll
  vim hello.sh
  cat hello.sh
  ll
  chmod 755 hello.sh 
  ll
  hello.sh
  ./ hello.sh
  ./hello.sh
  bash hello.sh
   hello.sh
  sh hello.sh
  history
[root@cdh1 shell-test]# history


[root@cdh1 shell-test]# history
   77  history
[root@cdh1 shell-test]# history
   77  history
[root@cdh1 shell-test]# ll
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# history
   77  history
   78  ll
   79  history
[root@cdh1 shell-test]# history -c
[root@cdh1 shell-test]# history
   77  history
[root@cdh1 shell-test]# history
   77  history
[root@cdh1 shell-test]# ls 
hello.sh
[root@cdh1 shell-test]# ls -l
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# ll
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# ls -ltr
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# history
   77  history
   78  ls 
   79  ls -l
   80  ll
   81  ls -ltr
   82  history
[root@cdh1 shell-test]# !81
ls -ltr
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# !82
history
   77  history
   78  ls 
   79  ls -l
   80  ll
   81  ls -ltr
   82  history
   83  ls -ltr
   84  history
[root@cdh1 shell-test]# !!
history
   77  history
   78  ls 
   79  ls -l
   80  ll
   81  ls -ltr
   82  history
   83  ls -ltr
   84  history
[root@cdh1 shell-test]# !!
history
   77  history
   78  ls 
   79  ls -l
   80  ll
   81  ls -ltr
   82  history
   83  ls -ltr
   84  history
[root@cdh1 shell-test]# history
   77  history
   78  ls 
   79  ls -l
   80  ll
   81  ls -ltr
   82  history
   83  ls -ltr
   84  history
[root@cdh1 shell-test]# 


[root@cdh1 shell-test]# echo -e "ab\bc"
ac
[root@cdh1 shell-test]#   echo -e "abc"
abc
[root@cdh1 shell-test]#   echo -e "ab\bc"
ac
[root@cdh1 shell-test]#   echo -e "a\tb\tc\nd\te\tf"
a	b	c
d	e	f
[root@cdh1 shell-test]#   echo "a\tb\tc\nd\te\tf"
a\tb\tc\nd\te\tf
[root@cdh1 shell-test]#   echo -e "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
a	b	c
d	e	f
[root@cdh1 shell-test]# 


[root@cdh1 shell-test]#  echo -e \
>   "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
a	b	c
d	e	f
[root@cdh1 shell-test]#  echo -e   "\x61\t\x62\t\x63\n\x64\t\x65\t\x66"
a	b	c
d	e	f
[root@cdh1 shell-test]#  echo -e "\e[1;31m abcd \e[0m"
 abcd 
[root@cdh1 shell-test]# ll
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# cat hello.sh
#vi hello.sh
#!/bin/Bash
#The first program
# Author: shenchao (E-mail: shenchao@lampbrother.net)
echo -e "Mr. Shen Chao is the most honest man in LampBrother"
[root@cdh1 shell-test]# ./hello.sh
Mr. Shen Chao is the most honest man in LampBrother
[root@cdh1 shell-test]# sh hello.sh
Mr. Shen Chao is the most honest man in LampBrother
[root@cdh1 shell-test]#  hello.sh
bash: hello.sh: command not found
[root@cdh1 shell-test]# 
[root@cdh1 shell-test]# bash hello.sh
Mr. Shen Chao is the most honest man in LampBrother
[root@cdh1 shell-test]# chmod a-x hello.sh 
[root@cdh1 shell-test]# ll
total 4
-rw-r--r-- 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# chmod a+x hello.sh 
[root@cdh1 shell-test]# ll
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# chmod a-x hello.sh 
[root@cdh1 shell-test]# ll
total 4
-rw-r--r-- 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# chmod 755 hello.sh 
[root@cdh1 shell-test]# ll
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# 


[root@cdh1 shell-test]# cat hello.sh
#vi hello.sh
#!/bin/Bash
#The first program
# Author: shenchao (E-mail: shenchao@lampbrother.net)
echo -e "Mr. Shen Chao is the most honest man in LampBrother"
[root@cdh1 shell-test]# wc -c hello.sh 
164 hello.sh
[root@cdh1 shell-test]# wc -w hello.sh 
23 hello.sh
[root@cdh1 shell-test]# wc -l hello.sh 
5 hello.sh
[root@cdh1 shell-test]# ls ; date ; pwd
hello.sh
Wed Jul 13 23:55:32 PDT 2016
/usr/local/shell-test
[root@cdh1 shell-test]# ls /root/test || echo "no
> 
> ;
> "
ls: cannot access /root/test: No such file or directory
no

;

[root@cdh1 shell-test]# ls /root/test || echo "no"
ls: cannot access /root/test: No such file or directory
no
[root@cdh1 shell-test]# ls /root/test && echo "yes"
ls: cannot access /root/test: No such file or directory
[root@cdh1 shell-test]# ls /root/test && echo "yes"
ls: cannot access /root/test: No such file or directory
[root@cdh1 shell-test]# ls test && echo "yes"
ls: cannot access test: No such file or directory
[root@cdh1 shell-test]# ls test ll echo "no"
ls: cannot access test: No such file or directory
ls: cannot access ll: No such file or directory
ls: cannot access echo: No such file or directory
ls: cannot access no: No such file or directory
[root@cdh1 shell-test]# ls test || echo "no"
ls: cannot access test: No such file or directory
no
[root@cdh1 shell-test]# ls test && echo "yes"
ls: cannot access test: No such file or directory
[root@cdh1 shell-test]# 
[root@cdh1 shell-test]# ll
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# s test && echo "yes" || echo "no"
bash: s: command not found
no
[root@cdh1 shell-test]# ls test && echo "yes" || echo "no"
ls: cannot access test: No such file or directory
no
[root@cdh1 shell-test]# ls test && echo "yes" || echo "no"
ls: cannot access test: No such file or directory
no
[root@cdh1 shell-test]# ll
total 4
-rwxr-xr-x 1 root root 164 Jul 13 23:25 hello.sh
[root@cdh1 shell-test]# mkdir test
[root@cdh1 shell-test]# ll
total 8
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
drwxr-xr-x 2 root root 4096 Jul 14 00:00 test
[root@cdh1 shell-test]# ls test && echo "yes" || echo "no"
yes
[root@cdh1 shell-test]# ls test
[root@cdh1 shell-test]# ls -l test
total 0
[root@cdh1 shell-test]# ls test && echo "yes" || echo "no"
yes
[root@cdh1 shell-test]# touch test/file-test.txt
[root@cdh1 shell-test]# ls -l test
total 0
-rw-r--r-- 1 root root 0 Jul 14 00:01 file-test.txt
[root@cdh1 shell-test]# ls test && echo "yes" || echo "no"
file-test.txt
yes
[root@cdh1 shell-test]# netstat -an | grep "ESTABLISHED
> ^C
[root@cdh1 shell-test]# netstat -an | grep "ESTABLISHED"
tcp        0      0 127.0.0.1:9000              127.0.0.1:51137             ESTABLISHED 
tcp        0      0 127.0.0.1:51137             127.0.0.1:9000              ESTABLISHED 
tcp        0      0 ::ffff:127.0.0.1:8031       ::ffff:127.0.0.1:37041      ESTABLISHED 
tcp        0      0 ::ffff:127.0.0.1:37041      ::ffff:127.0.0.1:8031       ESTABLISHED 
[root@cdh1 shell-test]# netstat -an
unix  3      [ ]         STREAM     CONNECTED     9937   @/var/run/hald/dbus-EGc9boEf4P
unix  3      [ ]         STREAM     CONNECTED     9936   
unix  3      [ ]         STREAM     CONNECTED     9917   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     9916   
unix  2      [ ]         DGRAM                    9878   
unix  3      [ ]         STREAM     CONNECTED     9821   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     9820   
unix  2      [ ]         DGRAM                    9819   
unix  3      [ ]         STREAM     CONNECTED     9768   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     9767   
unix  3      [ ]         STREAM     CONNECTED     9758   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     9757   
unix  2      [ ]         DGRAM                    9751   
unix  3      [ ]         STREAM     CONNECTED     9727   /var/run/dbus/system_bus_socket
unix  3      [ ]         STREAM     CONNECTED     9726   
unix  3      [ ]         STREAM     CONNECTED     9722   
unix  3      [ ]         STREAM     CONNECTED     9721   
unix  3      [ ]         DGRAM                    7176   
unix  3      [ ]         DGRAM                    7175 
...
...
...
...  
[root@cdh1 shell-test]# netstat -an | grep "ESTABLISHED"
tcp        0      0 127.0.0.1:9000              127.0.0.1:51362             ESTABLISHED 
tcp        0      0 127.0.0.1:51362             127.0.0.1:9000              ESTABLISHED 
tcp        0      0 127.0.0.1:9000              127.0.0.1:51137             ESTABLISHED 
tcp        0      0 127.0.0.1:51137             127.0.0.1:9000              ESTABLISHED 
tcp        0      0 ::ffff:127.0.0.1:8031       ::ffff:127.0.0.1:37041      ESTABLISHED 
tcp        0      0 ::ffff:127.0.0.1:37041      ::ffff:127.0.0.1:8031       ESTABLISHED 
[root@cdh1 shell-test]# 


[root@cdh1 shell-test]# cat hello.sh 
#vi hello.sh
#!/bin/Bash
#The first program
# Author: shenchao (E-mail: shenchao@lampbrother.net)
echo -e "Mr. Shen Chao is the most honest man in LampBrother"
[root@cdh1 shell-test]# cp hello.sh h.tx 
[root@cdh1 shell-test]# ll
total 12
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  164 Jul 14 00:08 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
[root@cdh1 shell-test]# sh h.tx 
Mr. Shen Chao is the most honest man in LampBrother
[root@cdh1 shell-test]# vim h.tx 
[root@cdh1 shell-test]# grep -n "world" h.tx 
1:world
[root@cdh1 shell-test]# grep -n "echo" h.tx 
5:echo -e "Mr. Shen Chao is the most honest man in LampBrother"
[root@cdh1 shell-test]# grep -n --color "echo" h.tx 
5:echo -e "Mr. Shen Chao is the most honest man in LampBrother"
[root@cdh1 shell-test]# cat h.tx 
world
#!/bin/Bash
#The first program
# Author: shenchao (E-mail: shenchao@lampbrother.net)
echo -e "Mr. Shen Chao is the most honest man in LampBrother"
[root@cdh1 shell-test]# ll
total 12
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
[root@cdh1 shell-test]# touch abc
[root@cdh1 shell-test]# ll
total 12
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
[root@cdh1 shell-test]# ll
total 12
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
[root@cdh1 shell-test]# touch 012
[root@cdh1 shell-test]# ll
total 12
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
[root@cdh1 shell-test]# ls ?abc
ls: cannot access ?abc: No such file or directory
[root@cdh1 shell-test]# ls ?bc
abc
[root@cdh1 shell-test]# ls ?abc
ls: cannot access ?abc: No such file or directory
[root@cdh1 shell-test]# ls *abc
abc
[root@cdh1 shell-test]# ls [0-9]*
012
[root@cdh1 shell-test]# ll
total 12
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
[root@cdh1 shell-test]# ls [^0-9]*
abc  hello.sh  h.tx

test:
file-test.txt
[root@cdh1 shell-test]# echo $(date)
Thu Jul 14 00:54:09 PDT 2016
[root@cdh1 shell-test]# date
Thu Jul 14 00:54:13 PDT 2016
[root@cdh1 shell-test]# echo `ls`
012 abc hello.sh h.tx test
[root@cdh1 shell-test]# echo ls
ls
[root@cdh1 shell-test]# echo ls
ls
[root@cdh1 shell-test]# echo 'ls'
ls
[root@cdh1 shell-test]# echo "ls"
ls
[root@cdh1 shell-test]# echo 'ls'
ls
[root@cdh1 shell-test]# echo `ls`
012 abc hello.sh h.tx test
[root@cdh1 shell-test]# date
Thu Jul 14 00:57:15 PDT 2016
[root@cdh1 shell-test]# echo `date`
Thu Jul 14 00:57:22 PDT 2016
[root@cdh1 shell-test]# echo `date`
Thu Jul 14 01:27:44 PDT 2016
[root@cdh1 shell-test]# name=sc
[root@cdh1 shell-test]# echo $name
sc
[root@cdh1 shell-test]# echo `$name`
bash: sc: command not found

[root@cdh1 shell-test]# echo "$name"
sc
[root@cdh1 shell-test]# echo '$name'
$name
[root@cdh1 shell-test]# echo $date

[root@cdh1 shell-test]# echo $(date)
Thu Jul 14 01:29:36 PDT 2016
[root@cdh1 shell-test]# echo "$(date)"
Thu Jul 14 01:29:42 PDT 2016
[root@cdh1 shell-test]# echo '$(date)'
$(date)
[root@cdh1 shell-test]# echo `$(date)`
bash: Thu: command not found

[root@cdh1 shell-test]# echo ‘$(date)'
> 
> 
> 
> ^C
[root@cdh1 shell-test]# 
[root@cdh1 shell-test]# name="shen chao"
[root@cdh1 shell-test]# name
bash: name: command not found
[root@cdh1 shell-test]# name
bash: name: command not found
[root@cdh1 shell-test]# $name
bash: shen: command not found
[root@cdh1 shell-test]# echo $name
shen chao
[root@cdh1 shell-test]# aa=123
[root@cdh1 shell-test]# echo $aa
123
[root@cdh1 shell-test]# aa="$aa"456
[root@cdh1 shell-test]# echo $aa
123456
[root@cdh1 shell-test]# aa=${aa}789
[root@cdh1 shell-test]# echo $aa
123456789
[root@cdh1 shell-test]# aa=$aa456
[root@cdh1 shell-test]# echo $aa

[root@cdh1 shell-test]# aa="$aa"456
[root@cdh1 shell-test]# echo $aa
456
[root@cdh1 shell-test]# echo $aa
456
[root@cdh1 shell-test]# aa=${aa}789
[root@cdh1 shell-test]# echo $aa
456789
[root@cdh1 shell-test]# aa="$aa"456
[root@cdh1 shell-test]# echo $aa
456789456
[root@cdh1 shell-test]# set aa
[root@cdh1 shell-test]# 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'
CLASSPATH=.:/user/local/jdk/lib:/user/local/jdk/jre/lib
COLORS=/etc/DIR_COLORS
COLORTERM=gnome-terminal
COLUMNS=157
DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-0fUn3bcLqe,guid=bcfba732c788ead6dd1ebda000000229
DESKTOP_SESSION=gnome
DIRSTACK=()
DISPLAY=:0.0
EUID=0
GDMSESSION=gnome
GDM_KEYBOARD_LAYOUT=us
GDM_LANG=en_US.UTF-8
GNOME_DESKTOP_SESSION_ID=this-is-deprecated
GNOME_KEYRING_PID=2383
GNOME_KEYRING_SOCKET=/tmp/keyring-Tmor2J/socket
GROUPS=()
GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2
G_BROKEN_FILENAMES=1
HADOOP_HOME=/user/local/hadoop-2.6.0
HADOOP_HOME_WARN_SUPPRESS=1
HBASE_HOME=/user/local/hbase-0.98.20-hadoop2
HISTCONTROL=ignoredups
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HIVE_HOME=/usr/local/hive
HOME=/root
HOSTNAME=cdh1
HOSTTYPE=i386
ID=0
IFS=$' \t\n'
JAVA_HOME=/user/local/jdk
JRE_HOME=/user/local/jdk/jre
KAFKA_HOME=/user/local/kafka_2.9.2-0.8.1.1
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=39
LOGNAME=root
MACHTYPE=i386-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
MAVEN_HOME=/usr/local/apache-maven-3.3.9
OLDPWD=/usr/local
OPTERR=1
OPTIND=1
ORBIT_SOCKETDIR=/tmp/orbit-root

这样查看变量无效
[root@cdh1 shell-test]# set aa;
[root@cdh1 shell-test]# 

[root@cdh1 shell-test]# echo $aa
456789456
[root@cdh1 shell-test]# set aa
[root@cdh1 shell-test]# set aa*
[root@cdh1 shell-test]# echo $name
shen chao
[root@cdh1 shell-test]# set >>vari.txt
[root@cdh1 shell-test]# ll
total 20
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rw-r--r-- 1 root root 7296 Jul 14 02:14 vari.txt
查看变量
[root@cdh1 shell-test]# grep -n --color "name" vari.txt 
88:name='shen chao'
[root@cdh1 shell-test]# wc -l vari.txt 
131 vari.txt

[root@cdh1 shell-test]# 
[root@cdh1 shell-test]# grep -n --color "name" vari.txt 
88:name='shen chao'
[root@cdh1 shell-test]# set >>vari.txt
[root@cdh1 shell-test]# set >>vari.txt
[root@cdh1 shell-test]# echo $name
shen chao
[root@cdh1 shell-test]# unset name
[root@cdh1 shell-test]# echo $name

[root@cdh1 shell-test]# set >>vari.txt
应该找不到变量值的,但是 追加的所以还是能看到。
[root@cdh1 shell-test]# grep -n --color "name" vari.txt 
88:name='shen chao'
219:name='shen chao'
350:name='shen chao'
覆盖
[root@cdh1 shell-test]# set >vari.txt
[root@cdh1 shell-test]# grep -n --color "name" vari.txt 
[root@cdh1 shell-test]# 


[root@cdh1 shell-test]# echo $name
shen chao
[root@cdh1 shell-test]# \d
bash: d: command not found
[root@cdh1 shell-test]# su hadoop
[hadoop@cdh1 shell-test]$ id
uid=500(hadoop) gid=500(hadoop) groups=500(hadoop)
[hadoop@cdh1 shell-test]$ echo $*

[hadoop@cdh1 shell-test]$ echo $*0
0
[hadoop@cdh1 shell-test]$ echo $0
bash
[hadoop@cdh1 shell-test]$ echo $1

[hadoop@cdh1 shell-test]$ $#
bash: 0: command not found
[hadoop@cdh1 shell-test]$ ll
total 20
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[hadoop@cdh1 shell-test]$ vim test$.h

[1]+  Stopped                 vim test$.h
[hadoop@cdh1 shell-test]$ vim test.h

[2]+  Stopped                 vim test.h
[hadoop@cdh1 shell-test]$ ll
total 20
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[hadoop@cdh1 shell-test]$ vim test.sh

[3]+  Stopped                 vim test.sh
[hadoop@cdh1 shell-test]$ touch test.sh
touch: cannot touch `test.sh': Permission denied
[hadoop@cdh1 shell-test]$ su - root
Password: 

^Z
[4]+  Stopped                 su - root
[hadoop@cdh1 shell-test]$ exit;
exit
There are stopped jobs.
[hadoop@cdh1 shell-test]$ exit
exit
Vim: Caught deadly signal TERM
Vim: preserving files...
Vim: Caught deadly signal TERM
Vim: preserving files...
Vim: Finished.
Vim: Caught deadly signal TERM
Vim: preserving files...
Vim: Finished.
[root@cdh1 shell-test]# Vim: Finished.
ll
total 20
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# id
uid=0(root) gid=0(root) groups=0(root)
[root@cdh1 shell-test]# ]d
bash: ]d: command not found
[root@cdh1 shell-test]# \d
bash: d: command not found
[root@cdh1 shell-test]# vim test$.sh
[root@cdh1 shell-test]# l
bash: l: command not found
[root@cdh1 shell-test]# ll
total 24
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rw-r--r-- 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# chmod a+x test\$.sh 
[root@cdh1 shell-test]# ll
total 24
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# sh test\$.sh 12 34 
46
[root@cdh1 shell-test]# cat test\$.sh 
#!/bin/bash
num1=$1
num2=$2
sum=$(( $num1 + $num2))
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# cp test\$.sh test1.sh
[root@cdh1 shell-test]# cat test1.sh 
#!/bin/bash
num1=$1
num2=$2
sum=$(( $num1 + $num2))
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# vim test1.sh 
[root@cdh1 shell-test]# ll
total 28
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root  113 Jul 14 02:44 test1.sh
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# sh test1.sh 23 34
test1.sh: line 5: 23: command not found

[root@cdh1 shell-test]# cat test1.sh 
#!/bin/bash
num1=$1
num2=$2
#sum=$(( $num1 + $num2))
sum= $num1 + $num2
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# vim test1.sh 
[root@cdh1 shell-test]# ll
total 28
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root  116 Jul 14 02:46 test1.sh
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# cat test1.sh 
#!/bin/bash
num1=$1
num2=$2
#sum=$(( $num1 + $num2))
sum= $($num1 + $num2)
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# sh test1.sh 23 34
test1.sh: line 5: 23: command not found

[root@cdh1 shell-test]# vim test1.sh 
[root@cdh1 shell-test]# cat test1.sh 
#!/bin/bash
num1=$1
num2=$2
#sum=$(( $num1 + $num2))
sum= $(($num1)) + $(($num2))
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# sh test1.sh 23 34
test1.sh: line 5: 23: command not found

[root@cdh1 shell-test]# vim test1.sh 
[root@cdh1 shell-test]# vim test1.sh 
[root@cdh1 shell-test]# sh test1.sh 23 34
test1.sh: line 5: 57: command not found

[root@cdh1 shell-test]# vim test1.sh 
[root@cdh1 shell-test]# sh test1.sh 23 34
test1.sh: line 5: 57: command not found

[root@cdh1 shell-test]# ll
total 28
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root  119 Jul 14 02:51 test1.sh
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# vim test1.sh 
[root@cdh1 shell-test]# sh test1.sh 23 34
57
[root@cdh1 shell-test]# cat test1.sh 
#!/bin/bash
num1=$1
num2=$2
#sum=$(( $num1 + $num2))
sum=$(( $num1 + $num2))
#变量sum的和是num1加num2
echo $sum
[root@cdh1 shell-test]# pwd
/usr/local/shell-test
[root@cdh1 shell-test]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2             5.7G  4.9G  545M  91% /
tmpfs                 504M  292K  503M   1% /dev/shm
/dev/sda4             7.9G  4.4G  3.2G  59% /user
/dev/sda1             291M   32M  245M  12% /boot
[root@cdh1 shell-test]# ll
total 28
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root  118 Jul 14 02:52 test1.sh
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# vim echoTest.sh
'[root@cdh1 shell-test]# ll
total 32
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rw-r--r-- 1 root root  149 Jul 14 03:15 echoTest.sh
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root  118 Jul 14 02:52 test1.sh
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# chmod a+x echoTest.sh 
[root@cdh1 shell-test]# ll
total 32
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  149 Jul 14 03:15 echoTest.sh
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root  118 Jul 14 02:52 test1.sh
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# sh echoTest.sh 1 3 4 6 78 90 234
The parameters is: 1 3 4 6 78 90 234
[root@cdh1 shell-test]# cat echoTest.sh
#!/bin/bash

for i in "$*"
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次
do
echo "The parameters is: $i"
done
[root@cdh1 shell-test]# cp echoTest.sh echoTest1.sh 
[root@cdh1 shell-test]# ll
total 36
-rw-r--r-- 1 root root    0 Jul 14 00:52 012
-rw-r--r-- 1 root root    0 Jul 14 00:45 abc
-rwxr-xr-x 1 root root  149 Jul 14 03:23 echoTest1.sh
-rwxr-xr-x 1 root root  149 Jul 14 03:15 echoTest.sh
-rwxr-xr-x 1 root root  164 Jul 13 23:25 hello.sh
-rwxr-xr-x 1 root root  157 Jul 14 00:28 h.tx
drwxr-xr-x 2 root root 4096 Jul 14 00:01 test
-rwxr-xr-x 1 root root  118 Jul 14 02:52 test1.sh
-rwxr-xr-x 1 root root   93 Jul 14 02:37 test$.sh
-rw-r--r-- 1 root root 7283 Jul 14 02:20 vari.txt
[root@cdh1 shell-test]# vim echoTest1.sh 
[root@cdh1 shell-test]# sh echoTest1.sh 1 3 4 6 78 90 234
The parameters is: 1
The parameters is: 3
The parameters is: 4
The parameters is: 6
The parameters is: 78
The parameters is: 90
The parameters is: 234
[root@cdh1 shell-test]# cat echoTest1.sh 
#!/bin/bash

for i in "$@"
#$*中的所有参数看成是一个整体,所以这个for循环只会循环一次
do
echo "The parameters is: $i"
done
[root@cdh1 shell-test]# 

[root@cdh1 shell-test]# $$
bash: 4004: command not found
[root@cdh1 shell-test]# echo $$
4004
[root@cdh1 shell-test]# echo $!

[root@cdh1 shell-test]# echo $$
4004
[root@cdh1 shell-test]# echo $?
0
[root@cdh1 shell-test]# $$
bash: 4004: command not found
[root@cdh1 shell-test]# echo $?
127
[root@cdh1 shell-test]# 

$?
最后一次执行的命令的返回状态。如果这个变量的值为0,证明上一个命令正确执行;
如果这个变量的值为非0(具体是哪个数,由命令自己来决定),则证明上一个命令执行不正确了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

5icode.top

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值