vim选中多行缩进(python多行缩进)与删除多行前面的空格--命令行脚本传参实现输入2个整数

vim选中多行缩进(python多行缩进)与删除多行前面的空格

最近用vim写python,有时候会在一段代码前面套一个循环的操作,这个时候将这一段代码整体向后平移四个空格,来满足vim缩进的要求,如何做到这一点呢?

  1. ESC之后,ctrl+v进入多行行首选中模式

  2. 使用上下键进行上下移动,选中多行行首

  3. shift+i,进入插入模式

  4. 连续敲击4个空格(注意在敲击空格按键的时候,注意这个时候不能够多行立即缩进,显示的是仅仅一行进行缩进,在执行第五步的时候,才会出现多行缩进的效果)

  5. 然后按esc按键,即可发现,你选中的行都缩进了

有时候从别的地方拷贝一段代码,粘贴到python代码中,过于多行缩进,不够美观,这个时候需要选中多行,删除多行前面的四个空格,方法也很简单

  1. ctrl+v进入块模式,然后上下左右移动,选中多行以及多个空格

  2. 按del按键即可删除

原文链接:https://blog.youkuaiyun.com/u013517182/article/details/93047412


shell基础练习题:使用read交互输入,命令行脚本传参2种方式,实现输入2个整数数字,并计算加减乘除。考察shell基础知识包括:变量定义、read、if判断语句、正则表达式等知识;

第一种方式:read交互输入参数

思路为:判断输入的第2个变量是否为空,为空则提示输入2个数字;不为空则判断输入的是否为整数,用到expr,作用为让2个变量进行相加,如果结果为0说明输入2个为数字,如结果非0则说明输入非整数,提示输入的不是非整数;

#!/bin/bash

read -p “pls input two number:” a b

if [ ! -z $b ]

then

expr $a + $b &>/dev/null

if [ $? -eq 0 ]

then

echo “a-b= ( ( (( ((a-$b))”

echo “a+b= ( ( (( ((a+$b))”

echo “ab= ( ( (( ((a$b))”

echo “a/b= ( ( (( ((a/$b))”

else

echo “input is not zhengshu”

fi

else

echo “you must input two number”

fi

执行结果如下:输入字母会报错,输入不是整数;只输入1个参数也会报错;

[linuxidc@localhost ~]$ sh a.sh

pls input two number:4 2

a-b=2

a+b=6

a*b=8

a/b=2

[linuxidc@localhost ~]$ sh a.sh

pls input two number:a 2

input is not zhengshu

[linuxidc@localhost ~]$ sh a.sh

pls input two number:10

you must input two number

脚本有bug,如果输入3个参数的话会报错如下:

[linuxidc@localhost ~]$ sh a.sh

pls input two number:1 3 3

a.sh: line 3: [: 3: binary operator expected

you must input two number

针对上面的脚本bug修改如下:

思路为:多添加一个变量c,并多了if判断,先判断 a 是 否 为 空 , 如 为 空 提 示 输 入 2 个 数 字 并 退 出 ; 然 后 判 断 a是否为空,如为空提示输入2个数字并退出;然后判断 a2退b是否为空,如未空提示输入2个数字并退出;只有$a b 都 不 为 空 即 都 有 输 入 值 , 再 判 断 b都不为空即都有输入值,再判断 bc是否为空,如未空执行下面的整数判断,如$c不为空同样提示输入2个数字并退出;

#!/bin/bash

read -p “pls input two number:” a b c

if [ -z $a ]

then

echo “you must input two number”

exit

elif [ -z $b ]

then

echo “you must input two number”

exit

fi

if [ -z $c ]

then

expr $a + $b &>/dev/null

if [ $? -eq 0 ]

then

echo “a-b= ( ( (( ((a-$b))”

echo “a+b= ( ( (( ((a+$b))”

echo “ab= ( ( (( ((a$b))”

echo “a/b= ( ( (( ((a/$b))”

else

echo “input is not zhengshu”

fi

else

echo “you must input two number”

fi

执行结果如下,什么都不输入,输入一个字符都会提示必须输入2个数字;输入2个值中有字母提示输入的非整数;

[linuxidc@localhost ~]$ sh a.sh

pls input two number:

you must input two number

[linuxidc@localhost ~]$ sh a.sh

pls input two number:1

you must input two number

[linuxidc@localhost ~]$ sh a.sh

pls input two number:1 a

input is not zhengshu

[linuxidc@localhost ~]$ sh a.sh

pls input two number:2 1

a-b=1

a+b=3

a*b=2

a/b=2

第二种方式:命令行脚本传参方式

思路为:定义a b两个变量,接受命令行传递的参数;$#为输入参数的总个数;判断输入的参数个数不等于2,则提示必须输入2个数字;等于2的话执行下面的脚本;

[linuxidc@localhost ~]$ cat b.sh

#!/bin/bash

a=$1

b=$2

if [ $# -ne 2 ]

then

echo “you must input two number”

exit 1

else

expr $a + $b &>/dev/null

if [ $? -eq 0 ]

then

echo “a-b= ( ( (( ((a-$b))”

echo “a+b= ( ( (( ((a+$b))”

echo “ab= ( ( (( ((a$b))”

echo “a/b= ( ( (( ((a/$b))”

else

echo “input is not zhengshu”

exit 1

fi

fi

执行结果如下:传参为空,参数为3个都会提示必须输入2个数字;传参包含非数字则提示输入的不是整数;

[linuxidc@localhost ~]$ sh b.sh 3 a

input is not zhengshu

[linuxidc@localhost ~]$ sh b.sh 3 2 3

you must input two number

[linuxidc@localhost ~]$ sh b.sh 3 2

a-b=1

a+b=5

a*b=6

a/b=1

总结:

read可以和用户交互性较好,脚本较为复杂多了if判断效率不高;

命令行传参使用表达式判断输入参数,执行效率和理解性较好;
https://blog.youkuaiyun.com/weixin_33914255/article/details/116559928

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值