SHELL脚本学习(二)

  • 在shell中计算整数表达式
    var=$((表达式))
$ var=$((2019 + 2019))
$ echo $var                       
4038

$ var=$(($(date +%Y)+$(date +%Y)))
$ echo $var                       
4038
  • test测试文件属性或数字
  • 与脚本信息有关的参数
    $0代表路径文件名,$1、$2、$3…表示脚本参数
    $#表示参数个数
    $@ 表示【 “$1” “$2” “$3” 】列出所有参数
    $* 表示【"$1"c"$2"c"$3"】,c为分隔符,默认为空格,用分隔符列出所有参数
    shift n 用于从左至右偏移n个参数
/path/to/script.sh arg1 arg2 arg3
$0                 $1   $2    $3

$ vim script.sh
#!/bin/bash
echo $0
echo $#
echo $1
echo $2
echo $3
echo $@
echo $*
shift 1
echo $#
echo $@

$ sh ./script.sh 123 343 hhh
./script.sh
3
123
343
hhh
123 343 hhh
123 343 hhh
2
343 hhh
写过的脚本
  • 用于删除文件名中的空格
#! /bin/bash
#去除当前文件夹下的flac文件的文件名中的空格

#for filename in $(find *.flac | sed "s/.*$/'&'/")

#find all files with .flac extension
for filename in *.flac
do
	# delete whitespace
	new=$(echo $filename|sed 's/ //g')
	#filename=$(echo $filename | sed 's/ /\\ /g')
	#filename=$(echo $filename| sed 's/.*/"&"/g')
	echo "$filename"
	echo $new
	
	# change filename
	mv "$filename" $new
done
  • 统计当前文件夹下的代码行数
# read file extension
read -t 30 -p "Please input file name:"  file

# count code lines with exact file extension
codelines=$(find  ./ -name "$file" | xargs cat | grep -v ^$ | wc -l)
echo $codelines
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值