美文网首页
shell入门21-移动位置参数shift

shell入门21-移动位置参数shift

作者: 万州客 | 来源:发表于2023-01-04 21:08 被阅读0次

shift,我之前真的没用过呢~

一,介绍

image.png
image.png

二,练习

[root@127 shell_demo]# vim parameter.sh
[root@127 shell_demo]# bash parameter.sh 
arg1=, arg2=, arg3=, arg4=, arg5=, arg6=, count=0
arg1=, arg2=, arg3=, arg4=, arg5=, arg6=, count=0
arg1=, arg2=, arg3=, arg4=, arg5=, arg6=, count=0
arg1=, arg2=, arg3=, arg4=, arg5=, arg6=, count=0
[root@127 shell_demo]# bash parameter.sh a b c 1 2 3
arg1=a, arg2=b, arg3=c, arg4=1, arg5=2, arg6=3, count=6
arg1=b, arg2=c, arg3=1, arg4=2, arg5=3, arg6=, count=5
arg1=1, arg2=2, arg3=3, arg4=, arg5=, arg6=, count=3
arg1=2, arg2=3, arg3=, arg4=, arg5=, arg6=, count=2
[root@127 shell_demo]# cat parameter.sh 
#!/bin/bash
# 功能描述:演示shift命令的作用,左移位置参数

echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, count=$#"
shift
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, count=$#"
shift 2
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, count=$#"
shift 1
echo "arg1=$1, arg2=$2, arg3=$3, arg4=$4, arg5=$5, arg6=$6, count=$#"
[root@127 shell_demo]# vim empty.sh
[root@127 shell_demo]# touch a.txt b.txt
[root@127 shell_demo]# echo "111" > 1.txt
[root@127 shell_demo]# echo "222" > 2.txt
[root@127 shell_demo]# mkdir test
[root@127 shell_demo]# bash empty.sh a.txt 1.txt b.txt 2.txt test/
a.txt 为空文件,正在删除该文件. 
1.txt为非空文件。
b.txt 为空文件,正在删除该文件. 
2.txt为非空文件。
test/为文件夹。
[root@127 shell_demo]# cat empty.sh 
#!/bin/bash
# 功能描述:读取位置参数,测试是否空文件并删除空文件

if [ $# -eq 0 ]; then
  echo "用法:$0 文件名..."
  exit 1
fi

# 测试位置变量个数,个数为0时循环结束
while (($#)); do
  if [ ! -s $1 ]; then
    echo -e "\033[31m$1 为空文件,正在删除该文件. \033[0m"
    rm -rf $1
  else
    [ -f $1 ] && echo -e "\033[32m$1为非空文件。\033[0m"
    [ -d $1 ] && echo -e "\033[32m$1为文件夹。\033[0m"
  fi
  shift
done

相关文章

  • shell编程学习笔记(二)

    内置命令:shift:使位置参数向左移动,默认移动1位,例如:shift 1。 expect是一门tcl(Tool...

  • Linux Shell编程之Bash中的位置参数

    什么是位置参数 位置参数指的是 shell 脚本的命令行参数,同时也表示 shell 函数的函数参数。Bash中的...

  • Shell Bash 中的位置参数和特殊参数

    Bash 中的位置参数是由除 0 以外的一个或多个数字表示的参数。 位置参数是当 Shell 或 Shell 的函...

  • processon

    shift + 上下箭头:调整主题位置ctrl + 上下箭头:移动试图ctrl + /:折叠、展开

  • shell 参数解析

    shell如何向c一样解析参数?可以使用shift命令,如下: while [ $# -ge 3 ] do i...

  • VS code 自定义快捷输入

    位置 ctrl+shift+p 搜索: snippets 输入类型: 比如html、javascript 参数说明...

  • 传参@bash

    众志成城 介绍 流动的数据是程序的肉体。 位置参数 执行shell程序时,shell会自动将第一个参数保存在特殊的...

  • Shell编程-05-Shell中的特殊变量和扩展变量

    特殊变量     在Shell中的特殊变量主要分别两种位置参数变量、状态变量两种。 位置参数变量     Shel...

  • IDEA 快捷键

    alt+shift 然后用鼠标左键点击文本,可以让光标在多个位置出现 alt+shift+↑ 本行上下移动 ctr...

  • 2020-09-03 VSCdoe 一些常用快捷键

    代码对其 command + [ , command + ] 单行 左右移动 一个tab 键的位置 shift ...

网友评论

      本文标题:shell入门21-移动位置参数shift

      本文链接:https://www.haomeiwen.com/subject/ghkccdtx.html