shell script

1.Demo

#!/bin/bash
echo hello world
$ chmod a+rx script.sh
$ ./script.sh

2.Variables-Part I

· No space around the “=” sign
· read (Automatically places quotes around its input)
· Scope of Variables
· export
· source a script via the “.”(dot) command
· enclose the variable itself in curly brackets

3.Variable-Part II

· $0 is the basename of the program as it was called.
· $1 … $9 are the first 9 additional parameters the script was called with.
· $@ is all parameters $1 … whatever.
· $*, is similar, but does not preserve any whitespace, and quoting
· $# is the number of parameters the script was called with.
· $? is the exit value of the last run command
· $$ is the PID (Process IDentifier) of the currently running shell.
· $! is the PID of the last run background process. This is useful to keep track of the process as it gets on with its job.
· IFS is the Internal Field Separator. The default value is SPACE TAB NEWLINE

4.WildCards

5.Escape Characters

Most characters(*,’,etc) are not interpreted by means of placing them in double quotes.",$,` and \ are interpreted by the shell

6.Loops

① For Loops

#!/bin/bash
for i in hello 1 * 2 goodbye
do
	echo "Looping ... i is set to $i"
done

② While Loops

#!/bin/bash
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do 
	echo "bye to quit"
	read INPUT_STRING
done

The colon(:)always evaluates to true.

7.Case

The case statement saves going through a whole set of if … then … else statements

#!/bin/sh

echo "Please talk to me ..."
while :
do
  read INPUT_STRING
  case $INPUT_STRING in
	hello)
		echo "Hello yourself!"
		;;
	bye)
		echo "See you again!"
		break
		;;
	*)
		echo "Sorry, I don't understand"
		;;
  esac
done
echo 
echo "That's all folks!"

8.Test

#!/bin/sh
if [ "$X" -lt "0" ]
then
  echo "X is less than zero"
fi
if [ "$X" -gt "0" ]; then
  echo "X is more than zero"
fi
[ "$X" -le "0" ] && \
      echo "X is less than or equal to  zero"
[ "$X" -ge "0" ] && \
      echo "X is more than or equal to zero"
[ "$X" = "0" ] && \
      echo "X is the string or number \"0\""
[ "$X" = "hello" ] && \
      echo "X matches the string \"hello\""
[ "$X" != "hello" ] && \
      echo "X is not the string \"hello\""
[ -n "$X" ] && \
      echo "X is of nonzero length"
[ -f "$X" ] && \
      echo "X is the path of a real file" || \
      echo "No such file: $X"
[ -x "$X" ] && \
      echo "X is the path of an executable file"
[ "$X" -nt "/etc/passwd" ] && \
      echo "X is a file which is newer than /etc/passwd"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值