shell脚本使用技巧3--函数调用

定义函数

function fname()

{

  statements;

}

或者

fname()

{

  statements;

}

传递参数给函数:

fname arg1 arg2;

ex:

  函数参数定义:

fname()

{

  echo $1, $2;

  echo "[email protected]";

  echo "$*";

  return 0;  

}

检测命令是否成功执行

#!/bin/bash
#filename:success_test.sh
cmd="ls"
$cmd
if [ $? -eq 0 ];
then
    echo "$cmd executed successfully"
else
    echo "$cmd terminated unsuccessfully"
fi
时间: 2024-08-09 14:28:23

shell脚本使用技巧3--函数调用的相关文章

shell脚本使用技巧3--调试

1.使用-x,开启shell脚本的跟踪调试功能 ex:bash -x script.sh or sh -x script.sh 2.使用set -x 和 set +x对脚本进行部分调试(输入中间的内容) #!/bin/bash #filename:debug.sh for i in {1..6}; do set -x echo $i set +x done echo "script executed" 3.固定格式生成调试信息 注:符号:告诉shell不要进行任何操作 #!/bin/b

收集的48个Shell脚本小技巧

0. shell 调试 复制代码 代码如下: sh -x somefile.sh 在somefile.sh 文件里加上set+x set-x 1. 用 && || 简化if else 复制代码 代码如下: gzip -t a.tar.gz if [[ 0 == $? ]]; then     echo "good zip" else     echo "bad zip" fi 可以简化为: 复制代码 代码如下: gzip  -t a.tar.gz &

shell脚本使用技巧2

0--stdin标准输入 1--stdout标准输出 2--stderr标准错误 重定向 echo "this is a good idea " > temp.txt temp.txt内容会被首先清空后再输入"this is a good idea" 追加 echo "this is a bad idea " >> temp.txt cat temp.txt 打印退出状态:echo $? ls + 2>out.txt 2输

shell脚本调试技巧

0.专业脚本的规范格式 函数1 函数2 main 函数 main $* 1.使用dos2unix命令转换来自windows下开发的脚本 dos2unix默认是没有安装的,可以yum 安装 # dos2unix web_jk.sh dos2unix: converting file web_jk.sh to UNIX format ... 2.使用echo 命令调试 3.使用bash命令参数调试 # sh [-nvx] scripts.sh -n    不会执行该脚本,仅查询脚本语法是否有问题,并

shell脚本使用技巧5--字符分隔

#!/bin/bash #filename:ifs.sh data="name,sex,rollon,location" oldIFS=$IFS IFS=, for item in $data; do echo Item: $item done #IFS=$oldIFS 设置IFS为,号分隔符 #!/bin/bash #filename:fenge.sh line="root:x:0:0:root:/root:bin/bash" oldIFS=$IFS IFS=&q

shell脚本小技巧

输入参数错误时,退格会出现^H,这个时候只要在脚本顶部加一条语句:stty erase ^h就可以了 #!/bin/sh stty erase ^h

shell脚本使用技巧7--cat

cat是单词concatenate缩写 echo 'text through stdin' | cat - file.txt 输出:text throgh stdin 和file.txt中的内容: cat -s file:压缩空白行 cat -T file:标记处文件中所有的制表符为^| cat -n line.txt :输出的内容添加行号(不会改变文件的内容的)

一个具有菜单选项的简单shell脚本

这是一个简单的shell脚本,里面包含了很多的shell脚本编写技巧,虽然脚本是从别的视频教程里面整理而来,但是对我的以后的编写shell脚本的道路有了一定的铺垫. #!/bin/bash ############# # main menu # ############# main_menu () { echo echo dis_mainmenu="CREATE MINISITE IN CHINAITLAB.COM" curdate=`date "+%Y-%m-%d %T&q

SHELL脚本编程的常识和VI常用技巧

来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc37518085 Shell脚本编程的常识... 3 七种文件类型... 3 正则表达式... 3 字符类描述... 4 shell的引号类型... 4 变量设置时的不同模式:... 4 条件测试... 5 命令执行顺序... 6 脚本调试... 6 一些常用的小trick.. 6 打印一些头信息... 6 创建