shell

Bash 在线工具

数组 list

echo "shell定义数组"

#定义数组(空数组)
#list=()
list=("value1" "value2" "value3")
echo "list=("value1" "value2" "value3")"

#打印元素
echo ${list[1]}

#打印所有index
echo ${!list[*]}

#打印所有元素
echo ${list[*]}

#数组增加一个元素
list=("${list[@]}" "value3")
echo "list=("${list[@]}" "value3")"

#打印所有元素
echo ${list[*]}

字典 dictionary

#!/bin/bash

echo "shell定义字典"

#声明字典
declare -A dic
dic=([key1]="value1" [key2]="value2" [key3]="value3")

#打印指定key的value
echo ${dic["key1"]}

dic["key4"]="value4"

#打印所有key
echo ${!dic[*]}

#打印所有value
echo ${dic[*]}

#遍历字典
#for key in $(echo ${!dic[*]})
for key in ${!dic[*]}
do
	echo "$key : ${dic[$key]}"
done

字典 单词统计

#! /bin/bash

echo "编写测试用: 输入格式为一行一个单词"
file=`pwd`/test.txt
echo 123 >> $file
echo 123 >> $file
echo 123 >> $file
echo 1 >> $file
echo 1 >> $file
echo 1 >> $file
echo 2 >> $file
echo 123 >> $file
echo 1 >> $file
echo 1 >> $file

echo "----打印测试用例----"
cat $file

#定义字典类型变量
declare -A dict

while read word
do
	#若dict[word]存在,value++;否则,加入字典value设为1
	if [ -n dict[${word}] ];then
		((dict[${word}]++))
	else
		dict[${word}]=1
	fi
done < $file

echo "----打印字典内容----"
echo "dictionary: "
echo "keys:" ${!dict[*]}
echo "values: " ${dict[*]}

echo "----打印统计结果----"
echo -e "word\t\tcount"
for key in $(echo ${!dict[*]})
do
	echo -e "$key\t\t\t${dict[$key]}"
done

echo "删除测试用例"
rm $file
	字符串变量表达式
	if  [ -n $string  ]             如果string 非空(非0),返回0(true)  
	整数变量表达式
	if [ int1 -ne int2 ]           如果不等于

在wordcount这脚本中, if [ -n dict[${word} ]这个判断会一直返回TRUE;

#若dict[word]存在,value++;否则,加入字典value设为1
if [ -n dict[${word}] ];then
	((dict[${word}]++))
else
	dict[${word}]=1
fi

这一段,可以用((dict[${word}]++))替换掉。
可以改成判断字符串长度是否为0if [ 0 -ne ${#dict[${word}]} ]

统计子文件夹文件数

 sh count.sh CUB_200_2011/images
#!/bin/sh
numOfArgs=$#
if [ $numOfArgs -ne 1 ]; then
  echo -e "Usage: \nbash $0 dirForCount"
  exit -1
fi
# args
ROOTDIR=$1
# core part
find $ROOTDIR -maxdepth 1 -type d | sort | while read dir; do
	count=$(find "$dir" -type f | wc -l)
	echo "$dir: $count"
done
	应用:统计CUB中每类图片个数

服务器相关脚本

统计程序运行时间

#!/bin/bash

startTime=`date +%Y%m%d-%H:%M`
start_s=`date +%s`
#---------------------------------------#

cd ZeroShot/gZSL
python run_revise_eval.py --config ../experiments/CUB/G/VGG19_UC/config.yaml --model_path ../../UC_LFNet.pth

#---------------------------------------#
end_s=`date +%s`
endTime=`date +%Y%m%d-%H:%M`

sumTime=$[ $end_s - $start_s ]
echo "start time: "$startTime
echo "end time: "$endTime
echo "total time: $((sumTime/3600))h $((sumTime/60))m $((sumTime%60))s"

监控服务器显卡使用情况(程序最大使用显存)

sh check_gpu_usage.sh 0 # 监控第1块显卡
#/bin/bash

numOfArgs=$#
if [ $numOfArgs -ne 1 ]; then
    echo -e "Usage: \nbash $0 [0-7]"
    exit -1
fi

# args
gpuNo=$(expr 9 + $1 \* 3)

maxValue=0
while [ true ]; do 
    output=`nvidia-smi | head -n $gpuNo | tail -n 1 | awk '{print $9}'`    
	OIFS=$IFS; IFS="M"; set -- $output; value=$1; unit=$2; IFS=$OIFS
	if [ $value -ge $maxValue ]; then 
		maxValue=$value
		echo $maxValue
	fi
	sleep 2;
done

修改训练数据大小

选取训练集前1000张图片

cp train.txt train.txt.bak
cat train.txt.bak | head -n 1000 > train.txt

定时刷新

  • 程序输出 watch -n 1 python train.py
  • GPU占用情况 gpustat -i

shell问题

Syntax error: Bad for loop variable

原因: 从 ubuntu 6.10 开始,ubuntu 就将先前默认的bash shell 更换成了dash shell;其表现为 /bin/sh 链接倒了/bin/dash而不是传统的/bin/bash。

方案: 将默认shell更改为bash。

sudo dpkg-reconfigure dash
#在选择项中选No
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值