1.坑:初始化时候注意oracle中更新字段值为空的
10,15g/^/s//\#/
awk '{print $0","}' test.txt
cat test.txt|grep -v ^\'|awk '{print $1}'|awk '{print $0","}'|xargs
坑:初始化时候注意oracle中更新字段值为空的
10,15g/^/s//\#/
awk '{print $0","}' test.txt
cat test.txt|grep -v ^\'|awk '{print $1}'|awk '{print $0","}'|xargs
2.数值对比使用-ne -eq -lt -gt
if [ 1 -ne 2 ]
字符串之间对比使用 < >
if [[ $a < $ b ]]
3.写好的shell脚本设置成命令执行方法‘
(1)使用别名:alias 'a=/tmp/a.sh'
(2)把脚本拷贝到/bin/目录:sudo cp /home/blue/mvmv.sh /bin/mvmv.sh
(3)把脚本拷贝到/usr/local/bin/目录即可
4.在linux中如果调用java程序
把开发好的程序打包成jar包,放在相关目录,比如说:/appcom/apps/commonlib,在shell脚本中把这个路径加入到 CLASSPATH变量中即可调用
5.自动输密码
expect -c "
set timeout 1;
spawn scp -P 22 user@remoteHost:/tmp/file.txt ~/ ;
expect {
yes/no { send \"yes\r\"; exp_continue }
*assword* { send \"password\r\" }
} ;
expect user@* {send \" ls -l \r\" } ;
expect user@* { send exit\r } ;
expect 100% ;
expect eof ;
"
expect -c "set timeout -1;
spawn ssh
[email protected];
expect {
*assword* { send \"xxxx\r\"};
};
expect lizhao535@* { send \"sudo su - $userName\r\"};
"
#!/bin/bash
ip=192.168.1.20
passwd="xxxxx"
#/tt这个目录是不存在的
comm="ls /tt"
expect -c "spawn ssh $ip $comm
expect {
yes/no {send yes\r};
password: {send $passwd\r};
}
expect eof";
5.ftp函数
ftp:
ftp -i -n 192.168.21.46 <<EOF
user zjk zjk123
ls
EOF
6.日期循环
start_date=`date -d "+0 day 20170205" +%Y%m%d`
end_date=`date -d "+0 day 20170405" +%Y%m%d`
while [[] $start_date < $end_date ]]
do
start_date=`date -d "+1 day $start_date" +%Y%m%d`
done
7.echo
#!/bin/bash
while read line
do
echo -e "select ">>hxx.txt
echo -e " '${line}' as tableName, ">>hxx.txt
echo -e " ' ' as tableComment, " >>hxx.txt
echo -e " 'MDM' as sourceDB ,">>hxx.txt
echo -e " 'CDMBDATA' as sourceOwner,">>hxx.txt
echo -e " count(1) as tableQty,">>hxx.txt
echo -e " 'NA' as partitionType,">>hxx.txt
echo -e " ' ' as keys,">>hxx.txt
echo -e " '\${baseName}' as hiveDBName,">>hxx.txt
echo -e " '\${hadoopUserName}' as hiveDBUser,">>hxx.txt
echo -e " '日全量' as syncType,">>hxx.txt
echo -e " '\${syncDate}' as syncDate,">>hxx.txt
echo -e " '\${dateStr}' as bizDate,">>hxx.txt
echo -e " 'edw' as jobName,">>hxx.txt
echo -e " '\${queueName}' as queueName,">>hxx.txt
echo -e " 'scheduler' as runType">>hxx.txt
echo -e "from edw.${line}_delta where y='\${dateStr:0:4}' and m='\${dateStr:4:2}' and d='\${dateStr:6:2}'">>hxx.txt
echo -e " union all" >>hxx.txt
done <test.txt
8.把多行字符串转成一行
cat b.txt |xargs
9.把一个文件转换成UTF-8格式:
(1)iconv -f gb2312 -t utf8 mytext.txt -o utf8.txt
(2)vim 文件名.txt
输入 :e ++enc=gbk 强制用gbk打开
输入 :w ++enc=utf8 转换到utf8保存。
多个文件:
for i in *.txt; do iconv -f gb2312 -t utf-8 $i.txt > ${i}.utf8.txt; done
10.mysql查询表拼接:
#!/bin/bash
a='('
while read line
do
a=${a}\'${line}\',
echo $a
done <b.txt
11.获取变量的值的变量的值:
tableName1=ODS_CIF2_LIFE_APPLY_PARTYNO_CI
tableName2=ODS_EGIS_ENDO_FEE
columns1=A.party_no,A.client_no
columns2=A.DEPTNO,A.ENTNO
conditions1="A.PARTY_NO=B.PARTY_NO and A.client_no=b.client_no and A.branch_code=B.branch_code"
conditions2="A.ENTNO=B.ENTNO and A.AMT_TYPE=b.AMT_TYPE and A.BRNO=B.BRNO and A.CERTNO=b.CERTNO and A.PAY_SRC=B.PAY_SRC"
for i in `seq 1 2`
do
tableNameTmp=tableName$i
tableName=$(eval echo '$'$tableNameTmp)
echo $tableName
columnsaTmp=columns$i
columns=$(eval echo '$'$columnsaTmp)
echo $columns
conditionsTmp=conditions$i
conditions=$(eval echo '$'$conditionsTmp)
echo $conditions
keysTmp=keys$i
keys=$(eval echo '$'$keysTmp)
echo $keys
done
12.查找两个文件的不同行:
(1)diff
diff 2.txt 1.txt |grep "<"|awk ' $1 = " " '
(2) comm -3 a.txt b.txt
comm - 12 就只显示在两个文件中都存在的行;
comm - 23 只显示在第一个文件中出现而未在第二个文件中出现的行;
comm - 123 则什么也不显示。
1 不显示只在第1个文件里出现过的列。
-2 不显示只在第2个文件里出现过的列。
-3 不显示只在第1和第2个文件里出现过的列。
(3)grep -v -f a.txt c.txt
(4)awk
awk 'NR==FNR{a[$0]++} NR>FNR&&!a[$0]' 1.txt 2.txt 找出不同的
awk 'NR==FNR{a[$0]++} NR>FNR&&a[$0]' 1.txt 2.txt 找出相同的
解释:NR==FNR{a[$0]++} 打开第一个文件的每一行
NR>FNR&&!a[$0] 打开第二个文件读取每一行是否等于第一文件中的任意一行
NR==FNR{a[$1]=$0;next} 打开第一个文件,把每行内容存入到a[$1]这个数组。
NR>FNR{if($1 in a)print $0"\n"a[$1]} 然后打开第二个文件,读取b.txt一行内容,然后插入一行第一个文件的内容。
写多个选项带参数的时候提醒某些选项不需要带参数:
set -- `getopt hq:f:d:t:sp "$*"` #:表示选项后面必须带有参数,-h和-s和-p选项后面不需要带参数