shell笔记一(函数和参数)

#!/bin/bash
function test_command() {
  echo "The params list in test is $@"
  echo "The params count in test is $#"
  echo "The \$1 cmd is $1"
  cmd=$1
  echo "==========$cmd execute output below ========"
  $cmd  # run the command
  ret=$?
  echo "==========$cmd execute output finish ========"
  if [ $ret -eq 0 ]
  then
    echo "$cmd executed successfully"
  else
    echo "$cmd executed failed"
  fi
  echo "============================================"

}

echo "The program params list:$@"
echo "The program params count is $#"
test_command "$1"
test_command $1

测试 ls -l命令

codefire@aliyun:~/workspace/shell_script$ ./success_test.sh "ls -l"

The program params list:ls -l
The program params count is 1
The params list in test is ls -l
The params count in test is 1
The $1 cmd is ls -l
==========ls -l execute output below ========
total 4
-rw-rw-r-- 1 codefire codefire   0 Nov 19 14:50 a.txt
-rwxrwxrwx 1 codefire codefire 572 Nov 19 15:37 success_test.sh
==========ls -l execute output finish ========
ls -l executed successfully
============================================
The params list in test is ls -l
The params count in test is 2
The $1 cmd is ls
==========ls execute output below ========
a.txt  success_test.sh
==========ls execute output finish ========
ls executed successfully

============================================


测试a+b


codefire@aliyun:~/workspace/shell_script$ ./success_test.sh "a+b"
The program params list:a+b
The program params count is 1
The params list in test is a+b
The params count in test is 1
The $1 cmd is a+b
==========a+b execute output below ========
./success_test.sh: line 8: a+b: command not found
==========a+b execute output finish ========
a+b executed failed
============================================
The params list in test is a+b
The params count in test is 1
The $1 cmd is a+b
==========a+b execute output below ========
./success_test.sh: line 8: a+b: command not found
==========a+b execute output finish ========
a+b executed failed
============================================


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值