linux - 我的案例脚本不应该区分大小写并且想要传递字符串

标签 linux bash shell unix

#!/bin/bash

  helpFunc()
{

    echo  "Usage:

    $0 --arg1
    $0 --arg2 filename add/delete/replace "hi how are you"


    $0 --help
    "
}


function fileread()  {
pwd 
echo "Fileread Function"

if [ -f $filename ] 
 then 
    echo "file exists"
    filename=$1
    cat $1
    case "$1" in
                add|ADD)
                        read $1 
                         if [$2 == 'add' ]
                         then 
                          echo $3 >> $1
                          echo "add opeartion completed"
                          fi
                        shift
                        ;;
                delete|DELETE)
                        read $1
                        echo "echo delet string from file"
                        if [$2 == 'delete' ]
                         then 
                          sed '/$3/d' $1
                          echo "add opeartion completed"
                          fi
                        shift
                        ;;
                replace|REPLACE)
                        read $1
                        if [ $4 == 'replace' ]
                         then
                             sed "s/$5/$6/g" "$1" > temp.txt
                             else
                             echo "Error: Cannot read $1"
                              fi
                      shift
                      ;;

                esac
                shift
#   return 0
  else  
    echo "file not exist"
#return 1 

fi
}

while [ $# ]
do
 case "$1" in

  --a)

               if [ -z "$2" ]
               then
                echo "No argument supplied"
                echo "hello" && echo " current process id : $$ "
                else 
                echo -e "no need of sending arguments for --a"
                 helpFunc a;
                 exit 1
                 shift
                fi
                 ;;

    --b)

            if [ $( echo -e $2 | grep -c -E "^-" ) -eq 0 ]
            then
                fileName=$2
                if [ -f "$fileName" ]
                then
                    echo -e "The file is valid"
                    echo "Displaying $2 file..." 
             [ -z $fileName ] && { echo "File name missing"; exit 1; } || cat $fileName
              case "$filename" in
                add|ADD)
                        echo "adding"
                       fileread $3 $4 
                        shift
                        ;;
                delete|DELETE)
                        echo "echo deleting"
                        fileread
                        shift
                        ;;
                replace|REPLACE)
                     echo "replacing"
                       fileread
                       shift
                       ;;

                esac
                else
                    echo -e "please enter the valid file name or location"
                    helpFunc b;
                    exit 1
                    shift
                fi
            fi      


esac

shift

done

exit 0

最佳答案

函数文件读取应忽略大小写输入,如 ADD/add、delete/Delete 等。如果我提供 cmd 行参数和值,则执行操作,它应验证区分大小写、文件存在和文件路径

./script.sh -a -b/etc/opt/test.txt 添加“嗨,你好吗”删除“我很好”替换“嗨”“嘿”

关于linux - 我的案例脚本不应该区分大小写并且想要传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45156331/

相关文章:

linux - 参数未传递给 shell 脚本 copyfile.sh : 1: copyfile. sh: [d: 未找到

c++ - Linux系统中如何知道端口是否打开

java - 无法加载库 :/tmp/BridJExtractedLibraries978435834650156898/OpenIMAJGrabber. 所以

bash - 难以理解反引号内反斜杠的非明显使用

linux - 无法识别 Shell 脚本 -e 命令行参数

mysql - 用于大量使用 wordpress 博客的简单脚本解决方案(shell、perl、其他)?

linux - 用 bash 中的另一个子字符串替换一个子字符串

linux - SIGSEGV 崩溃但无法收集回溯

linux - 如果 GNU/Linux 中的实用程序是从 UNIX 复制而来的,它们怎么会是免费和开源的呢?

bash - 您将如何扩展此脚本以处理大于 255 的数字?