您的位置:首页 > 运维架构 > Shell

shell 脚本学习2

2015-11-16 14:30 615 查看

交互输入自动化

[cloudera@quickstart shell]$ head input.data
1
hello
# shell
[cloudera@quickstart shell]$ cat vim interactive.sh
cat: vim: No such file or directory
#!/bin/bash
#Filename": interactive.sh
read -p "Enter number:" no ;
read -p "Enter name:" name
echo You have entered $no, $name;
# 执行脚本
[cloudera@quickstart shell]$ ./interactive.sh < input.data
You have entered 1, hello
# 执行脚本2

[cloudera@quickstart shell]$ ./interactive.sh
Enter number:1
Enter name:hello
You have entered 1, hello


并行加速

#!/bin/bash

PIDARRAY=()
for file in *
do
# 后台计算
md5sum $file &
# 将进程的PID加入数组
PIDARRAY+=("$!")
echo "the PID is $!"
done
# 等待PIDs运行结束
wait ${PIDARRAY[@]}

[cloudera@quickstart shell]$ ./generate_checksms.sh
the PID is 34609
the PID is 34610
the PID is 34611
the PID is 34612
22b5e82ac7440fdb2f3c9b1dafa98f1f  input.data
0580cdb40b6d9ed2147c6e67f64eba7e  checkword.sh
9d76bfd8f1fa8964d24f5ae8ed40057e  interactive.sh
d356c563e70e94e00235a97fbba7dc55  generate_checksms.sh
[cloudera@quickstart shell]$ vim generate_checksms.sh


文件处理

文件的交集与差集

comm file1 file2 # 第一列是1的单词 第二咧是2的单词 第三列是共有的单词

#ex 输出1 2 中独有的单词
# comm 1 2 -3 | sed 's/^\t//'


查找并删除重复文件

# 有错误不知道错哪里了
#!/bin/bash
#Filename:remove_duplicated.sh
#set -x
ls -LS --time-style=long-iso | awk 'BEGIN {
getline;getline; # 调过第一行,读取第二行的name和size
name1=$8;size=$5
}
{
name2=$8;
if (size==$5)
{
"md5sum " name1 | getline; csum1=$1; # 用getline读取md5sum的外部命令的结果
"md5sum " name2 | getline; csum2=$1;
if ( csum1==csum2 )
{
print name1; print name2
}
};

size=$5; name1=name2;
}' | sort -u > duplicate_files # 删除重复行sort -u

cat duplicate_files | xargs -I {} md5sum {} | sort | uniq -w 32 | awk '{ print "^"$2"$" }' | sort -u > duplicate_sample

echo Removing
comm duplicate_files duplicate_sample -2 -3 | tee /dev/stderr | xargs rm
echo Removed duplicates files successfully.


文件权限, 所有权和黏贴位



# 文件所有权
chown user.group filename


不可改

chattr +i file
chattr -i file


生成空白文件

touch
# 生成100个文件
foname in {1..100}.txt
do
touch $name
done
# 设定时间戳
touch -d "Fri Jun 25 20:50:14 IST 1999" filename


符号链接

ln -s target symbolic_link_name
# 在用户目录中创建了一个名为Web的符号链接指向/var/www
ln -l -s /var/www/ ~/web


查找符号链接

ls -l | grep "^l"
find . -type l -print


打印符号链接路径

$readlink link


生成文件统计信息

[clz@localhost shell_learn]$ file /etc/passwd
/etc/passwd: ASCII text

[clz@localhost shell_learn]$ file -b /etc/passwd
ASCII text


生成文件统计信息

#!/bin/bash
#Filename: filestat.sh

if [ $# -ne 1 ]
then
echo "Usage is $0 basepath";
exit
fi
path=$1

declare -A statarray; # 创建关联数组

while read line;
do
ftype=`file -b "$line" | cut -d, -f1` #  分隔符逗号,取第一段数据
let statarray["$ftype"]++;# 记录文件类型的数量

done < <(find $path -type f -print) # 读取文件名 输出类型

echo ======================find types and counts ===============
for ftype in "${!statarray[@]}"
do
echo $ftype : ${statarray["$ftype"]} # 遍历输出
done

[clz@localhost shell_learn]$ ./filestat.sh .
======================find types and counts ===============
Vim swap file : 1
data : 11
Bourne-Again shell script : 10
ASCII text : 2


查找文件差异并进行修补

# diff
[clz@localhost shell_learn]$ diff -u diff.txt  diff2.txt
--- diff.txt    2015-11-15 21:42:17.010909687 -0800
+++ diff2.txt   2015-11-15 21:43:01.857780704 -0800
@@ -1,6 +1,7 @@ # - 减少的行 + 增加的行
1
2
-3
4
-5
-
+6
+7
+8
+9
#  把diff的结果写入patch
[clz@localhost shell_learn]$ diff -u diff.txt  diff2.txt  > diff.patch
# patch 做修复
[clz@localhost shell_learn]$ patch -p1 diff.txt < diff.patch
patching file diff.txt
[clz@localhost shell_learn]$ cat diff.txt
1
2
4
6
7
8
9
# 撤销修复
[clz@localhost shell_learn]$ patch -p1 diff.txt < diff.patch
patching file diff.txt
Reversed (or previously applied) patch detected!  Assume -R?
y
[clz@localhost shell_learn]$ cat diff.txt
1
2
3
4
5




head tail

# 打印除了第五行之外的所有行
seq 11 |head -n -5
1
2
3
4
5
6


tail命令可以实时跟踪文件

tail  -f filename


只列出目录的各种方法

ls -d */
ls -F | grep "/$"
ls -l | grep "^d"
find . -type d -maxdepth 1 -print


统计文件的行数,单词数和字符数

wc -w 单词数
-c 字符数
-l 行数
-L 打印出文件中最长一行的长度


目录树

tree
-P 打印出通配符的文件
—I 非通配符的文件
-h 显示文件大小


[clz@localhost shell_learn]$ tree . -hP "*.sh"
.
├── [  59]  cecho.sh
├── [ 101]  debug.sh
├── [  82]  digui.sh
├── [ 124]  fileexists.sh
├── [ 403]  filestat.sh
├── [ 109]  func.sh
├── [ 232]  IFSread.sh
├── [ 235]  rename.sh
├── [ 191]  sleep.sh
└── [ 159]  success_test.sh
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: