linux - bash 脚本中未显示参数

标签 linux bash shell sh

所以我必须为一个项目制作一个 bash 脚本,并且我们必须以一种可以使用参数以不同方式使用它的方式来制作它。以 bash morse.sh -e Text.txt 运行我的脚本,我的脚本不会注册任何参数,这意味着我有一个命令 echo $1 并且在以 sh morse.sh -e Text.txt 运行它时不会显示任何内容,脚本将识别我的参数(当然还有许多其他错误,但事实并非如此)。

#!/bin/bash

function encode {
echo $1
input=$(<$2)
uppercasemod=`echo $input | tr '[:lower:]' '[:upper:]'`
echo $uppercasemod > $2

for (( i=0; i<${#uppercasemod}; i++ ));
do
    char=`echo "${uppercasemod:$i:1}"`
    if [[ $char == 'A' ]]
    then
        sed -i 's/A/.-\t/' $2
    elif [[ $char == 'B' ]]
    then
        sed -i 's/B/-...\t/' $2
    elif [[ $char == 'C' ]]
    then
        sed -i 's/C/-.-.\t/' $2
    elif [[ $char == 'D' ]]
    then
        sed -i 's/D/-..\t/' $2
    elif [[ $char == 'E' ]]
    then
        sed -i 's/E/.\t/' $2
    elif [[ $char == 'F' ]]
    then
        sed -i 's/F/..-.\t/' $2
    elif [[ $char == 'G' ]]
    then
        sed -i 's/G/--.\t/' $2
    elif [[ $char == 'H' ]]
    then
        sed -i 's/H/....\t/' $2
    elif [[ $char == 'I' ]]
    then
        sed -i 's/I/..\t/' $2
    elif [[ $char == 'J' ]]
    then
        sed -i 's/J/.---\t/' $2
    elif [[ $char == 'K' ]]
    then
        sed -i 's/K/-.-\t/' $2
    elif [[ $char == 'L' ]]
    then
        sed -i 's/L/.-..\t/' $2
    elif [[ $char == 'M' ]]
    then
        sed -i 's/M/--\t/' $2
    elif [[ $char == 'N' ]]
    then
        sed -i 's/N/-.\t/' $2
    elif [[ $char == 'O' ]]
    then
        sed -i 's/O/---\t/' $2
    elif [[ $char == 'P' ]]
    then
        sed -i 's/P/.--.\t/' $2
    elif [[ $char == 'Q' ]]
    then
        sed -i 's/Q/--.-\t/' $2
    elif [[ $char == 'R' ]]
    then
        sed -i 's/R/.-.\t/' $2
    elif [[ $char == 'S' ]]
    then
        sed -i 's/S/...\t/' $2
    elif [[ $char == 'T' ]]
    then
        sed -i 's/T/-\t/' $2
    elif [[ $char == 'U' ]]
    then
        sed -i 's/U/..-\t/' $2
    elif [[ $char == 'V' ]]
    then
        sed -i 's/V/...-\t/' $2
    elif [[ $char == 'W' ]]
    then
        sed -i 's/W/.--\t/' $2
    elif [[ $char == 'X' ]]
    then
        sed -i 's/X/-..-\t/' $2
    elif [[ $char == 'Y' ]]
    then
        sed -i 's/Y/-.--\t/' $2
    elif [[ $char == 'Z' ]]
    then
        sed -i 's/Z/--..\t/' $2
    elif [[ $char == 1 ]]
    then
        sed -i 's/1/.----\t/' $2
    elif [[ $char == 2 ]]
    then
        sed -i 's/2/..---\t/' $2
    elif [[ $char == 3 ]]
    then
        sed -i 's/3/...--\t/' $2
    elif [[ $char == 4 ]]
    then
        sed -i 's/4/....-\t/' $2
    elif [[ $char == 5 ]]
    then
        sed -i 's/5/.....\t/' $2
    elif [[ $char == 6 ]]
    then
        sed -i 's/6/-....\t/' $2
    elif [[ $char == 7 ]]
    then
        sed -i 's/7/--...\t/' $2
    elif [[ $char == 8 ]]
    then
        sed -i 's/8/---..\t/' $2
    elif [[ $char == 9 ]]
    then
        sed -i 's/9/----.\t/' $2
    elif [[ $char == 0 ]]
    then
        sed -i 's/0/-----\t/' $2

    fi
done
counter=`grep -P '\t' *.txt | wc -w`
for (( i=0; i<counter; i++ ));
do
    sed -i 's/ //' *.txt
done
}

以前有人遇到过这个问题吗?

最佳答案

我会这样写,所以你只需要调用 sed 一次:

morse() {
    local -A morse=(
        [A]='.-'          [J]='.---'        [S]='...'         [2]='..---'
        [B]='-...'        [K]='-.-'         [T]='-'           [3]='...--'
        [C]='-.-.'        [L]='.-..'        [U]='..-'         [4]='....-'
        [D]='-..'         [M]='--'          [V]='...-'        [5]='.....'
        [E]='.'           [N]='-.'          [W]='.--'         [6]='-....'
        [F]='..-.'        [O]='---'         [X]='-..-'        [7]='--...'
        [G]='--.'         [P]='.--.'        [Y]='-.--'        [8]='---..'
        [H]='....'        [Q]='--.-'        [Z]='--..'        [9]='----.'
        [I]='..'          [R]='.-.'         [1]='.----'       [0]='-----'
    )
    local sed_commands=()
    for char in "${!morse[@]}"; do
        sed_commands+=( -e "s/$char/${morse[$char]}\\t/gi" )
    done
    # let sed consume stdin
    sed "${sed_commands[@]}"
}

然后你就可以了

$ echo "Hello world" | morse
....    .       .-..    .-..    ---      .--    ---     .-.     .-..    -..

关于linux - bash 脚本中未显示参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48154793/

相关文章:

regex - 如何将 "ip"字符串 grep 到文件中?

linux - 用于获取转发端口并插入到程序配置文件的 Bash 脚本

PHP 系统调用函数不能正确导出变量

linux - 将 init 转换为 systemd

shell - shell 脚本中的 boolean 变量

linux - Monogame (Linux) - 导入 fbx 资源

c++ - 使用指定的运行超时从 C++ 执行另一个程序

linux - bash 中的 Windows 兼容密码

c - 正确的 FIFO 客户端-服务器连接

Linux Red Hat 5.6 和 VNC : KDE & Gnome