#!/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 -lThe 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
============================================