shell基本命令与学习(笔记类似的,持续更)

内建命令
shell函数
外部命令


cd ..
cd
who
who | wc -l
ls
printf "i am %s,%s\n" Hello world
who | grep root
set -x
set +x


sh
sh -x
exit


ls -l | cut -c 1-10


sort passwd | uniq


sort passwd | uniq -c   //  -d    //  -u


echo Testing one two three | wc -c // -l  // -w


wc passwd


x=qwdqwd
echo there are ${#x} character in $x


echo first arg is $1
echo first arg is ${10}


i=5
echo $((i++)) $i
echo $((++i)) $i


x=abc ; printf "x is now '%s', enter new value: " $x;read x


cat /etc/passwd |
> while IFS=: read user pass uid gid fullname homedir shell
> do
> echo $user $uid
> done


echo $SHELL 




#利用vi编辑器编辑脚本,将脚本命名为脚本功能.sh
#Shell脚本不是复杂的程序,它是按行解释的,脚本第一行总是以 #!/bin/sh 开头,它通知系统以下的Shell程序使用系统上的Bourne Shell来解释。
#第二行注释中写入脚本名,第三行注释开始写入脚本功能——习惯。
#以下行进入脚本正式编写
#编写完后给脚本添加执行权限:
#     chmod u+x ScripName
#运行脚本:ScripName
#                或   ./ScripName


vi ShowHello.sh


#!/bin/sh
#ShowHello.sh
#To show hello to somebody
echo -n“Enter Your Name:”
read NAME
echo “Hello,$NAME!”  


chmod u+x showHello.sh
./showHello.sh




新建用户 
useradd UserName
为用户添加密码 
passwd UserName
删除用户 
userdel UserName  
新建组群
groupadd GroupName
删除组群 
groupdel GroupName


用户身份
whoami
查看登录用户
who[-a  -H]
查看用户操作
w
用户间切换
su UserName
创建目录(make directory)
   mkdir DirecName
删除目录(remove directory)
rmdir DirecName
显示工作目录(print working directory)
pwd
显示目录内容(list)
ls [选项] 
   选项:-a 显示当前目录下的所有文件,包括以.开头的文件。
        -l 以长列表的形式显示文件列表,如:
           -rw-r--r-- 1 haison haison 18 Nov 2 10:10 haison.c


显示文件内容
      more FileName 一次一屏的显示文件内容,空格或回车显示下一屏
      less FileName 一次一屏的显示文件内容,空格或回车显示下一屏
      cat FileName    一次全部显示文件的内容
             cat file1 file2>file3 将file1、file2的内容写到file3,覆盖file3原内容
             cat file1>>file2 将file1的内容追加到file2,file2原内容不变
      head FileName 只显示文件的前10行
               head –n FileName 显示文件的前n行
      tail FileName     只显示文件的后10行
              tail –n FileName 显示文件的后n行
       nl FileName     以编号的形式一次全部显示文件的内容,忽略空格


改变文件/目录的属主(change owner)
     chown Owner FileName/DirecName
     如:chown Dave haison.c
改变文件/目录的属组(change group)
    chgrp Group FileName/DirecName
     如:chgrp Dave haison.c
改变文件/目录的属主和属组 
    chown owner:group FileName/DirecName 
     如:chown Dave:Dave haison.c


使用 echo命令可以显示单个变量的取值,要在变量名前面加$
      如:USER=“haison” 
              echo $USER
如果设置变量时,不想再改变其值,可以将其设置为只读,只要在变量名前加readonly
使用set命令可以显示所有本地定义的shell变量
使用unset 变量名可以清除对变量的定义


  if  [ 条件1 ]
    then
        命令1
elif [ 条件2 ]
  then  
       命令2
  else 
       命令3
    fi   


  case 值 in
      模式1)
          命令1
          ;;


          *)
          命令n
          ;;
  esac


  for 变量名 in 列表
  do
    命令
  done
  
  until 条件
  do
      命令
  done


  while 命令
  do
    命令
  done


函数名 ()
{
  命令

}


n示例

#!/bin/sh

#ifTest

#to show the method ofif

echo -e "Enterthe first integer:\c"

read FIRST

echo -n "Enterthe second integer:"

read SECOND

if ["$FIRST" -gt "$SECOND" ]

    then

       echo "$FIRST is greater than$SECOND"

    elif [ "$FIRST"-gt "$SECOND" ]

then

    echo "$FIRST is less than$SECOND"

else

    echo "$FIRST is equal to $SECOND"

fi


n示例

#!/bin/sh

#caseTest

#to  test the method ofcase

USER=`whoami`

case $USER in   

    root)echo “You can doall the operations”

         ;; 

  Dave)echo "You cando some operations        

           ;;       

        *)echo "Sorry,you can not do anything"     

           ;;

esac


n示例

#!/bin/sh

#forTest

#to test the method offor

COUNTER=0

for FILES in *

do 

     COUNTER=`expr $COUNTER + 1`

done 

echo "There are$COUNTER files in `pwd` "


#!/bin/sh

#untilTest

#totest the mothod of until

IS_ROOT=`who|grep root`

until[ "IS_ROOT" ]

do 

    IS_ROOT=`who|greproot`   

    sleep 5

done

echo"Watch it.root in!"


n示例

#!/bin/sh

#whileTest

#to test the method of while

COUNTER=0

while [ $COUNTER  -lt 10 ]

do

   echo $COUNTER

   COUNTER=`expr $COUNTER + 1`

done

while read LINE

do

  echo $LINE

done < names.txt


n示例1

#!/bin/sh

#funTest

#to test the function

DATE=`date`

Hello()

{

  echo “Hello,today is $DATE”

}

Hello


#!/bin/sh

#funTest

#to test the function

. ./Hello

Hello






















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值