• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > shell基础学习中的字符串操作、for循环语句示例

shell基础学习中的字符串操作、for循环语句示例

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了shell基础学习中的字符串操作、for循环语句示例等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

echo $my_name
echo ${my_name}

# ------------------------------------
# 字符串操作
# ------------------------------------

# 单引号字符串的限制,双引号没有这些限制:
# 单引号里的任何字符都会原样输出,单引号字符串中的变量是无效的
# 单引号字串中不能出现单引号(对单引号使用转义符后也不行)
name="will"
age=24
my_full_name='${name}${age}'
echo ${my_full_name}
my_full_name="${name}${age}"
echo ${my_full_name}

# 字符串拼接
echo ${name}${age}

# 字符串长度
echo ${#name} # 4

# substring
message="I want to be healthy"
echo ${message:2} # want to be health, 2是position
echo ${message:2:4} # want,2是position,4是len

# delete shortest match from front: ${string#substring}
echo ${message#*want}
# delete shortest match from back: ${string%substring}
echo ${message%healthy}

# delete longest match from front: ${string##substring}
echo ${message##*h}
# delete longest match from back: ${string%%substring}
echo ${message%%t*}

# find and replace: ${string/pattern/replacement}
book_name="Catch Eye Eye"
echo ${book_name/Eye/Cat}
# find and replace all match: ${string//pattern/replacement}
echo ${book_name//Eye/Cat}

file_path="/usr/local/bin"
# only replace when pattern match the beginning: ${string/#pattern/replacement}
echo ${file_path/#\/usr/tmp}
# only replace when pattern match the end: ${string/%pattern/replacement}
echo ${file_path/%bin/tmp}

# string index
stringZ=abcABC123ABCabc
echo `expr index "$stringZ" C12` # Mac OSX不支持expr


# ------------------------------------
# 语句
# ------------------------------------

# if
if true
then
 echo "ok, true"
fi

# 写成一行
if true; then echo "ok"; fi

var='12'
if [ $var -eq 12 ]; then
    echo "This is a numeric comparison if example"
fi

if [ "$var" = "12" ]; then
    echo "This is a string if comparison example"
fi

if [[ "$var" = *12* ]]; then
    echo "This is a string regular expression if comparison example"
fi

name="jxq"
if [ "$name" = "jxq" ]; then
 echo "hello" $name
fi


# 循环语句
for item in `ls *.sh`
do
 echo $item
 echo "completed"
done

# 写成一行
for item in `ls *.sh`; do echo $item; echo "completed"; done;

counter=1
while :
do
 echo "bee"
 let "counter=$counter+1"
 if [ $counter -eq 3 ]; then
  break # break/continue与Java类似
 fi
done

# Case语句
opt="install"
case "${opt}" in
 "install" )
  echo "install..."
  exit

 
 "update" )
  echo "update..."
  exit

 
 *) echo "bad opt"
esac
</div>

</div>

您可能想查找下面的文章:

相关文章

  • 用shell脚本实现自动切换内网和外网实现高可用
  • 关于vi和vim的区别及命令详解
  • linux 创建守护进程的相关知识
  • 实现自动清除日期目录shell脚本实例代码
  • shell脚本实现批量采集爱站关键词库
  • Linux Shell脚本系列教程(二):终端打印命令详解
  • 杀掉oracle在线用户脚本分享
  • linux下命令行操作快捷键及技巧(分享)
  • 一天一个shell命令 linux文本操作系列-wc命令详解
  • 浅谈ubuntu 中sudo update与upgrade的作用及区别

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • Shell脚本批量重命名文件后缀的3种实现
    • 获取服务器信息的Shell脚本分享(ubuntu、centos测试通过)
    • linux系统下hosts文件详解及配置
    • sed删除文件中的一行内容的脚本代码
    • Linux shell常用的73条命令总结
    • 一个简单的linux命令 tail
    • Linux中10个有用的命令行补全例子
    • Shell、Perl、Python、PHP访问 MySQL 数据库代码实例
    • Linux Shell脚本系列教程(七):脚本调试
    • 完美解决ntp的错误问题no server suitable for synchronization fo

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有