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=":"
count=0
for item in $line;
do
    [ $count -eq 0 ] && user=$item;
    [ $count -eq 6 ] && shell=$item;
    let count++
done;
IFS=$oldIFS
echo $user\‘s shell is $shell;

注意:[ ]空格;

时间: 2024-08-09 14:28:25

shell脚本使用技巧5--字符分隔的相关文章

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脚本小技巧

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

shell脚本判断里面的字符含义

[ -s FILE ] 如果 FILE 存在且大小不为0则为真. [ -a FILE ] 如果 FILE 存在则为真. [ -b FILE ] 如果 FILE 存在且是一个块特殊文件则为真. [ -c FILE ] 如果 FILE 存在且是一个字特殊文件则为真. [ -d FILE ] 如果 FILE 存在且是一个目录则为真. [ -e FILE ] 如果 FILE 存在则为真. [ -f FILE ] 如果 FILE 存在且是一个普通文件则为真. [ -g FILE ] 如果 FILE 存在且

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脚本使用技巧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="l

shell脚本学习与总结

shell脚本学习总结,东西很多,供初学者参考. shell脚本是区分大小写的. 2.Unix特殊字符有: ( ; $ ? & * () [] ` ' " + 使用其时要进行转义() 3.Shell的注释以#开头 4.函数的定义Function fuction_name(){ Command to execute} 调用时直接用function_name. 5.控制结构 1)If...then语句 If [ test_command ]    Then    Commandsfi2)If