阅读背景:

将一个文件中的英文字母全部转为大写字母

来源:互联网 
#!/bin/bash
while [ "
#!/bin/bash
while [ "$1" ]
do
    if [ "$1"="-i" ]
    then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1"="-o" ]
    then
        outfile=$2
        echo $outfile
        shift 2
        echo $outfile
    else
        echo "Program $0 doesn't recognize option $1 "
    fi
done
echo $infile
echo $outfile
tr a-z A-Z <$infile> $outfile
#end

保存为upcase.
#./upcase -i a.txt -o d.txt |cat -n的时候显示如下:
1.a.txt
2.a.txt
3.d.txt
4.d.txt
5.d.txt
6.
不知道怎么回事,应该是infile变成了d.txt,outfile变成空了。


11 个解决方案

#1


显示还少了一行:./upcase: line 23: $outfile: ambiguous redirect
就是最后的转换没有成功,应该是没有目标文件导致的。

#2


#!/bin/bash
while [ "$1" ]; do
    if [ "$1" == "-i" ]; then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1" == "-o" ]; then
        outfile=$2
        echo $outfile
        shift 2
        echo $outfile
    else
        echo "Program $0 doesn't recognize option $1 "
    fi
done
echo $infile
echo $outfile
tr '[a-z]' '[A-Z]' <$infile >$outfile
#end

#3


引用 2 楼 ljc007 的回复:
Assembly code
#!/bin/bash
while [ "$1" ]; do
    if [ "$1" == "-i" ]; then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1" == "-o" ]; then
        outfi……


效果依旧一样

#4


引用 3 楼 aimyray 的回复:
效果依旧一样

#cat a.txt
topic.CSDN.net

#cat test.sh

#!/bin/bash
while [ "$1" ]; do
    if [ "$1" == "-i" ]; then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1" == "-o" ]; then
        outfile=$2
        echo $outfile
        shift 2
        echo $outfile
    else
        echo "Program $0 doesn't recognize option $1 "
    fi
done
echo $infile
echo $outfile
tr '[a-z]' '[A-Z]' <$infile >$outfile

#./test.sh -i a.txt -o d.txt
a.txt
a.txt
d.txt
d.txt
a.txt
d.txt

#cat d.txt
TOPIC.CSDN.NET

#5


在ubuntu下面是正常的。
redhat下的结果依然是这样哦

#6


直接用tr 命令不行吗

tr '[:lower:]' '[:upper:]' < yourfile > newfile

转换yourfile中的小写字母,转换结果写入文件newfile

#7


tr 'a-z' 'A-Z' 试试

#8


引用 6 楼 justkk 的回复:
直接用tr 命令不行吗

tr '[:lower:]' '[:upper:]' < yourfile > newfile

转换yourfile中的小写字母,转换结果写入文件newfile


直接tr命令可以。就是outfile这个变量的值怎么变为空了。而且我把这个同一个redhat 的虚拟机在另外一台机器上跑就没有问题了!!!

#9


if [ "$1"="-i" ]
等号前后要有空格吧

#10


都可以了,[ "$1"="-i" ]改成[ "$1" = "-i" ]就可以了,多两个空格就ok了

#11


引用 9 楼 justkk 的回复:
if [ "$1"="-i" ]
等号前后要有空格吧


是的!!
刚刚发现了!

" ]
do
    if [ "
#!/bin/bash
while [ "$1" ]
do
    if [ "$1"="-i" ]
    then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1"="-o" ]
    then
        outfile=$2
        echo $outfile
        shift 2
        echo $outfile
    else
        echo "Program $0 doesn't recognize option $1 "
    fi
done
echo $infile
echo $outfile
tr a-z A-Z <$infile> $outfile
#end

保存为upcase.
#./upcase -i a.txt -o d.txt |cat -n的时候显示如下:
1.a.txt
2.a.txt
3.d.txt
4.d.txt
5.d.txt
6.
不知道怎么回事,应该是infile变成了d.txt,outfile变成空了。


11 个解决方案

#1


显示还少了一行:./upcase: line 23: $outfile: ambiguous redirect
就是最后的转换没有成功,应该是没有目标文件导致的。

#2


#!/bin/bash
while [ "$1" ]; do
    if [ "$1" == "-i" ]; then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1" == "-o" ]; then
        outfile=$2
        echo $outfile
        shift 2
        echo $outfile
    else
        echo "Program $0 doesn't recognize option $1 "
    fi
done
echo $infile
echo $outfile
tr '[a-z]' '[A-Z]' <$infile >$outfile
#end

#3


引用 2 楼 ljc007 的回复:
Assembly code
#!/bin/bash
while [ "$1" ]; do
    if [ "$1" == "-i" ]; then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1" == "-o" ]; then
        outfi……


效果依旧一样

#4


引用 3 楼 aimyray 的回复:
效果依旧一样

#cat a.txt
topic.CSDN.net

#cat test.sh

#!/bin/bash
while [ "$1" ]; do
    if [ "$1" == "-i" ]; then
        infile="$2"
        echo $infile
        shift 2
        echo $infile
    elif [ "$1" == "-o" ]; then
        outfile=$2
        echo $outfile
        shift 2
        echo $outfile
    else
        echo "Program $0 doesn't recognize option $1 "
    fi
done
echo $infile
echo $outfile
tr '[a-z]' '[A-Z]' <$infile >$outfile

#./test.sh -i a.txt -o d.txt
a.txt
a.txt
d.txt
d.txt
a.txt
d.txt

#cat d.txt
TOPIC.CSDN.NET

#5


在ubuntu下面是正常的。
redhat下的结果依然是这样哦

#6


直接用tr 命令不行吗

tr '[:lower:]' '[:upper:]' < yourfile > newfile

转换yourfile中的小写字母,转换结果写入文件newfile

#7


tr 'a-z' 'A-Z' 试试

#8


引用 6 楼 justkk 的回复:
直接用tr 命令不行吗

tr '[:lower:]' '[:upper:]' < yourfile > newfile

转换yourfile中的小写字母,转换结果写入文件newfile


直接tr命令可以。就是outfile这个变量的值怎么变为空了。而且我把这个同一个redhat 的虚拟机在另外一台机器上跑就没有问题了!!!

#9


if [ "$1"="-i" ]
等号前后要有空格吧

#10


都可以了,[ "$1"="-i" ]改成[ "$1" = "-i" ]就可以了,多两个空格就ok了

#11


引用 9 楼 justkk 的回复:
if [ "$1"="-i" ]
等号前后要有空格吧


是的!!
刚刚发现了!

"="-i" ]     if [



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: