-2

So i had to make a bash script for a project and we had to make it in a way where we can use arguments to use it differently. Running my script as bash morse.sh -e Text.txt my script will not register any of my arguments meaning i have a command echo $1 and it wont show anything while running it as sh morse.sh -e Text.txt the script will recognise my arguments (of course with many others errors but thats not the case).

#!/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
}

Anyone had this problem before?

shellter
  • 36,525
  • 7
  • 83
  • 90
  • 1
    It's difficult to help you without the actual code and the actual error you're seeing. – ron rothman Jan 08 '18 at 17:04
  • See [Why is printf better than echo](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo). Specifically, `echo -e` may work differently with different shells. – Mark Plotnick Jan 08 '18 at 17:06
  • @MarkPlotnick Its not the echo command my problem. Nowhere in my code the $1 get recognised as -e – George Kiou Jan 08 '18 at 17:17
  • Please read https://stackoverflow.com/help/on-topic , https://stackoverflow.com/help/how-to-ask , https://stackoverflow.com/help/dont-ask , https://stackoverflow.com/help/mcve and take the [tour](https://stackoverflow.com/tour) before posting more Qs here. Good luck. – shellter Jan 08 '18 at 17:26
  • 1
    @shellter here is the code – George Kiou Jan 08 '18 at 17:27
  • 3
    Function arguments are not the same as script arguments. – chepner Jan 08 '18 at 17:27
  • read about `case $var in ; e1) do_cmd1 ;; e2) do_cmd2 ;; .... ;; esac` . You can do this much more simply like `case "$inputChar" in [Aa] ) printf ".-\t" ;; [Bb] printf ... ;; .... ;; esac` . Good luck. – shellter Jan 08 '18 at 17:31
  • @chepner you are right thank you, never thought of it. If you want you could make it as an answer so i can have the post as resolved :D – George Kiou Jan 08 '18 at 17:35

3 Answers3

2

I would write that like this, so you only need to invoke sed once:

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[@]}"
}

Then you can do

$ echo "Hello world" | morse
....    .       .-..    .-..    ---      .--    ---     .-.     .-..    -..
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
1

The immediate problem is (apparently, based on comments) that you are not passing parameters to the function you declared (though you didn't post the calling code, so this is vaguely speculative).

But a much more pressing problem is that you are calling sed to rewrite the entire file once for each character. This is horribly, almost criminally inefficient, and not at all how you use sed. This is a stream editor; the way you use it is you call it once with a script in the sed language where you specify what changes should be performed on the file. Like this:

sed 's/[Aa]/.-\t/g
    s/[Bb]/-...\t/g
    s/[Cc]/-.-.\t/g
    # etc etc
    s/ //g' filename

Add the -i option once you are satisfied that the script's output is more or less what you expect; I probably didn't quite understand every nuance of your approach (especially at the end, replacing the first space on every line in all text files repeatedly, the number of times you found words in the Morse output, did I get that at all right?)

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I see that Glenn Jackman posted a nicely encapsulated version of this while I composed my reply. His code ends up doing the same thing, though more obscurely. – tripleee Jan 08 '18 at 18:29
0

The problem is that the command echo is taking the -e argument as it own parameter. Checkout the example bellow, your argument is there and it works.

echo "all parameters => $*"

if [ "-e" == "$1" ]; then
   echo Fora Temer
fi

You should see the text: Fora Temer

Look this link to improve your argument processing.

Dimas Crocco
  • 410
  • 6
  • 16