shell中参数与grep

发布时间:2013-12-08 17:46:34 阅读:1250次

#!/bin/bash
echo "共"$#"参数"
echo "脚本名称"$0
echo "所有的参数"$*
echo "参数1"$1
echo "参数2"$2
#循环显示所有的参数
for i in $*
    do
        echo $i
        done
#变量b是否在变量a中
a=world
b=lld
echo $a | grep $b
#echo $?
#echo $? #0找到,>0没有找到
if [ $? -gt 0 ];
    then
    echo "没有找到"
    else
        echo "找到"
        fi
i=0;
echo $i;
let i=i+1
echo $i
cd /home
echo $(pwd)
ls=$(ls)
#echo $ls
for i in $ls
    do
echo $i | grep 'tar.gz'
    echo "result:"$?
#    then
#            echo "yes"
#        fi
    done

2011  whereis bash //查找bash的路径,用bash比较好
 2012  vim parameter.sh
 2013  chown +x parameter.sh
 2015  ./parameter.sh
 2018  who
 2020  echo `who`
 2024  echo $(who)

 2025  who='test';
 2026  echo $who
 2028  echo ${who};
 2029  echo ${#who};//取得字符串的长度

process.sh

#!/bin/bash
echo "共"$#"参数"
echo "脚本名称"$0
echo "所有的参数"$*
echo "参数1"$1
echo "参数2"$2
#循环显示所有的参数
for i in $*
    do
        echo $i
        done
#变量b是否在变量a中
a=world
b=lld
echo $a | grep $b
#echo $?
#echo $? #0找到,>0没有找到
if [ $? -gt 0 ];
    then
    echo "没有找到"
    else
        echo "找到"
        fi
i=0;
echo $i;
let i=i+1
echo $i
cd /home
echo $(pwd)
ls=$(ls)
#echo $ls
for i in $ls
    do
echo $i | grep 'tar.gz'
    echo "result:"$?
#    then
#            echo "yes"
#        fi
    done

test@acer:~$ ./process.sh test 123456 28
共3参数
脚本名称./process.sh
所有的参数test 123456 28
参数1test
参数2123456
test
123456
28
没有找到
0
1
/home
result:1
test.tar.gz
result:0
test@acer:~$


sort对文本文件排序
uniq对文本文件去重,一般先排序后去重
cat history.txt | wc
显示行,单词,字符数

cut剪切
cut -d ',' -f 1 /etc/passwd > 1.txt 添加到文件覆盖
cut -d ',' -f 1 /etc/passwd >> 1.txt添加到文件追加
添加文件中是多行

echo `cut -d ',' -f 3 cut.txt` >> cut_new.txt
添加到文件中是一行多列

join连接两个文件

tr替换
tr 'a-z' 'A-Z' < 1.txt > 1_new.txt

diff比较文件
vimdiff明确比较文件
test@acer:~$ sed -e '/^#/d' sed.sh 删除以#开头的行,显示
test@acer:~$ sed -n -e '/^#/p' sed.sh 删除不匹配的行,只显示以#开头的行
test@acer:~$ sed -e '/^[^#]/d' sed.sh 同上



if.sh
#!/bin/sh
read first
if [ $first > 2 ];then
    echo "大於2"
fi
i=10
if [ $i==10 ];then
    echo ten
fi

if [ -f $first ];then
    echo 文件
fi

for.sh
for((i=1;i<=10;i++));do
        echo $i;
    done;


read.sh
read text
echo "您輸入的是:".$text

while.sh
#!/bin/bash
i=0
while [ $i -lt  10 ]
 do
   echo $i
   i=`expr $i + 1`
 done

如有问题,可以QQ搜索群1028468525加入群聊,欢迎一起研究技术

支付宝 微信

有疑问联系站长,请联系QQ:QQ咨询

转载请注明:shell中参数与grep 出自老鄢博客 | 欢迎分享