Shell
卓耿Drogon
醉舞经阁半卷书,坐井说天阔
展开
-
bash 统计当前目录文件数量
#! /bin/bashsum=0for i in $(ls) # ls -r 反向排序doif [ -f $i ]#if [[ -f $i ]]#if test -f $ithen((sum+=1))echo $ifidoneecho "sum=$sum"返回主目录原创 2022-01-26 15:09:11 · 1458 阅读 · 0 评论 -
bash 冒泡排序
#! /bin/bash#排序整数,从小到大 冒泡排序read a b c dar=($a $b $c $d)for((i=0;i<=3;i++))dofor((j=0;j<=((3-1-$i));j++))doif [ ${ar[$j]} -gt ${ar[(($j+1))]} ]thend=${ar[(($j+1))]}ar[(($j+1))]=${ar[$j]}ar[$j]=$dfidonedoneecho "${ar[*]}"返回主目录...原创 2022-01-26 15:05:24 · 270 阅读 · 0 评论 -
bash 乘法表
#! /bin/bash#乘法表for((j=1;j<=9;j++))dofor((i=1;i<=$j;i++))doprintf "$i*$j=$(($i*$j)) "#echo -n "$i*$j=$(($i*$j)) " #这行也可以doneechodone脚本输出结果:1*1=11*2=2 2*2=41*3=3 2*3=6 3*3=91*4=4 2*4=8 3*4=12 4*4=161*5=5 2*5=10 3*5=15 4*5=20 5*5=251原创 2022-01-26 14:59:56 · 1111 阅读 · 0 评论 -
bash 猜数游戏
#! /bin/bash#猜数游戏#shell带有内置函数RANDOM可以随机生成随机数其范围为:0~32767 $RANDOMnum_valid=1num=0#-------------------------------------------------function guess_input(){#echo "please input num(1-999)"#read -n 3 num#num=$(($RANDOM%999 +1)) # $(())整数运算符num=$原创 2022-01-26 14:50:36 · 3402 阅读 · 0 评论 -
bash 调用python文件
pytest.pyfrom sys import argvdef f1(): print('this is f1.')if __name__ == '__main__': a = 'abc' b = 'xyz' c = a+b d = 'abc''xyz' print(c) print(d) print("argv[0]", argv[0]) for i in argv: print(i) #if le原创 2022-01-26 14:30:50 · 3119 阅读 · 0 评论 -
bash 将文件内容转置
#! /bin/bash#将文件内容转置#AWK 是一种处理文本文件的语言,是一个强大的文本分析工具cat test_data_transpose_file.txtecho ""echo "-------------------"if True;thena=$(awk '{print $1}' test_data_transpose_file.txt)b=$(awk '{print $2}' test_data_transpose_file.txt)echo $aecho $b#原创 2022-01-26 11:47:24 · 609 阅读 · 0 评论 -
bash 包含其它文件
#!/bin/bash#包含其它文件#. ./test_data_source.txtsource ./test_data_source.txtecho "--------------"echo $$echo "Language: $1"echo "URL: $2"echo "-------------"func_echo_array hellotest_data_source.txt文件如下#! /bin/bashfunction func_echo_array(){原创 2022-01-24 11:05:57 · 141 阅读 · 0 评论 -
bash 统计单词出现频率
# /bin/bash#统计单词出现频率#单词都是小写字母和' '单引号组成。单词之间有一个或多个空格组成。#test_data_words.txt#统计指定字符if True;thengrep -o -i '\bYOU\b' test_data_words.txt | wc #-o 或 --only-matching#grep -o -i '\bYOU\b' test_data_words.txt | wc -wfiecho "-----------------------"# 有原创 2022-01-24 10:52:04 · 469 阅读 · 0 评论 -
bash 显示有效的电话号码
#! /bin/bash#显示有效的电话号码#格式如下#(xxx) xxx-xxxx#xxx-xxx-xxxxgrep -P '^\(?1{1}[0-9]{2}\)?( |-)[0-9]{3}-{1}[0-9]{4}\s' test_data_number.txt-P, --perl-regexp PATTERN is a Perl regular expression13661242508136adfdafdaf136d568974025813526848136原创 2022-01-24 10:36:02 · 288 阅读 · 0 评论 -
bash 打印第10行
#! /bin/bash#显示line.txt的第10行if True;thenhead -n 10 test_data_line.txt| tail -n +10 #tail -n +10 就第10行到末尾fiif True;thentail -n +10 test_data_line.txt | head -n 1fiif True;thensed -n '10,10p' test_data_line.txtfiline1line2line3line4line5原创 2022-01-24 10:22:23 · 483 阅读 · 0 评论 -
bash 移动字符设备文件
#! /bin/bash#判断一文件是不是字符设备文件,如果是将其拷贝到 /dev 目录下echo "input filename "read filenameif [ -c $filename ]thencp $filename /devfi返回主目录原创 2022-01-24 09:55:47 · 254 阅读 · 0 评论 -
bash cp file
#! /bin/bash#author zhuofeng#cp mv rm 复制/移动一定大小的文件到指定目录tdirectory="temp"if ! [ -d $tdirectory ]thenecho "create directory"mkdir $tdirectoryfi#for i in $(find ./ -type f -size +0b) #for 语句是以空格为分隔符,所以遇到名字有空格的文件,就会有问题find ./ -type f -size +0b原创 2022-01-24 09:54:11 · 209 阅读 · 0 评论 -
bash break continue
#! /bin/bash#break n#continue nfunction test_break(){echo "input num:"while read num;docase $num in0)echo 'zero';;[1-5])echo "1-5";;[6-9])echo "6-9"break;;*)echo "other";;esacdone}test_break返回主目录原创 2022-01-24 09:40:39 · 247 阅读 · 0 评论 -
bash return
#!/bin/bashsum=0 #全局变量function getsum(){ for((i=$1; i<=$2; i++)); do ((sum+=i)) #改变全局变量 done echo "--" return $? #返回上一条命令的退出状态}read mread ngetsum $m $necho "The sum is $sum" #输出全局变量if getsum $m $n; then echo原创 2022-01-24 09:40:03 · 562 阅读 · 0 评论 -
bash select in
#! /bin/bashecho "select your os"select name in 'linux' 'iso' 'windows' 'unix'doecho "your select $name"done#回车将重新执行select语句#ctrl D 退出select的无限循环echo "after ctrl D"脚本输出结果:select your oslinuxisowindowsunix#? 1your select linux#? 7your原创 2022-01-24 09:39:46 · 90 阅读 · 0 评论 -
bash while for
#! /bin/bash#---------------------------------function test_while(){echo $0 'test_while_until'sum=0i=1#while((i<=100))until((i>100))do((sum+=i))((i+=1))doneecho sum=$sum}#---------------------------------function test_for_c(){ech原创 2022-01-21 11:40:02 · 182 阅读 · 0 评论 -
bash case in
#! /bin/bashtestfunction test(){printf "input a char:"read -n 1 aprintf "\n"case $a in[967]) echo '9 or 6 or 7' ;;a | A) echo 'a | A' ;;[0-9]) echo '0-9' ;;[a-z]) echo 'a-z' ;;[A-Z]) echo 'A-Z' ;;*) echo 'other' ;;esac}脚本输出结果原创 2022-01-21 11:36:54 · 514 阅读 · 0 评论 -
bash write2file
#! /bin/bashread filenameread urlif test -w $filename && [ -n $url ]then echo $url>$filename echo "write succ"else echo "write fail"fi脚本输出结果:返回主目录原创 2022-01-21 11:35:00 · 304 阅读 · 0 评论 -
bash if else
#! /bin/bashprintf "please input integer number:"read numif ((num ==1))thenecho "num is 1"elif ((num == 2))thenecho "num is 2"elif ((num == 3));thenecho "num is 3"elseecho "num is other"fi脚本输出结果:please input integer number:123num is othe原创 2022-01-21 11:28:44 · 299 阅读 · 0 评论 -
bash exit
#! /bin/bashecho "befor exit"exit 1echo "after exit"脚本输出结果:使用 bash ./test_exit.txtbefor exit使用 . test_exit.txt当前进程会退出。返回主目录原创 2022-01-21 11:26:09 · 453 阅读 · 0 评论 -
bash 数组操作
#! /bin/bash#数组操作testfunction test(){array1=(5 100 'www.baidu')array2=(a b 90)echo array1[1]=${array1[1]}echo len_a1=${#array1[*]}echo array1= ${array1[*]}echo "拼接"array3=(${array1[*]} ${array2[*]})echo ${array3[*]}echo "---"array1[5]=55原创 2022-01-21 11:16:32 · 645 阅读 · 0 评论 -
bash 函数调用
#! /bin/bash#函数function func(){echo "language: $1"echo "URL: $2"echo "total para is $#"}#调用函数func C++ http://c/echo $0 pid is $$脚本输出结果:language: C++URL: http://c/total para is 2/usr/bin/bash pid is 5708返回主目录...原创 2022-01-21 11:00:13 · 879 阅读 · 0 评论 -
bash shell实践目录
字符串的拼接与截取原创 2022-01-21 10:40:25 · 256 阅读 · 0 评论 -
bash 字符串的拼接与截取
#! /bin/bash#字符串拼接与截取function test(){url="https://www.baidu.com"a="index"b="$url$a" #拼接c="$url:$a" #拼接echo url=$urlecho a=$aecho $becho $clen=${#url}echo $lenecho "#截取右边字符"x=${url#*//}y=${url#https://}z=${url#*/}zz=${url##*/}echo $x,$原创 2022-01-21 10:18:44 · 1037 阅读 · 0 评论