Q:

Script getting stuck in a loop/freezing while using a awk command

In my program, I want to get substrings of the inputs to compare them and save them as variables however my script seems to be freezing/stuck in a loop.
#!/bin/bash

ram="5G";
file="forge.jar"
dir="MC/Eternal/"

echo $@

for var in $@
do
    begin= `awk -v awkvar="$var" '{print substr($awkvar,1,2)}'`
    end= `awk -v awkvar="$var" '{print substr($awkvar,3)}'`
    echo $begin $end
    if [ $begin = "-r" ]
    then
        echo Setting ram to $end
        ram=$end
    elif [ $begin = "-d" ]
    then
        echo "Looking for directory MC/$out"
        directory=$end
    elif [ $begin = "-f" ]
    then
        echo "Looking for file $out"
        file=$end
    fi
done
echo running command: java -Xms1G -Xmx$ram -jar $dir$file
echo Do you want to run it? [Y/n]:
read input
if [ input = "y" ]
    java -Xms1G -Xmx$ram -jar $dir$file
else
    echo cancelled
fi

When I input ./runserver.sh -r10G -dfoo -fmc.jar I expect to get java -Xms1G -Xmx10G -jar MC/foo/mc.jar ... but the program seems to get stuck on the begin= ... line. I've tried begin = awk '{print substr($awkvar,1,2)} and just begin = awk '{substr($awkvar,1,2)}' with the backticks around the awk statement. I am new to Linux and I have just successfully launched and connected to my Minecraft server. Any and all help is appreciated. 
Edit: Found that if I use the "${var:x:y}" instead of the awk function I can get a substring. However, it seems that the program is trying to run the arguments instead of making it a string by the error it gives me ./runserver.sh: line 11: -r: command not found

A:

You are not allowed any blanks on shell assignments to variables. So begin= ``awk...  is just initialising begin to empty and then trying to run the output of awk as a command. Same for end=
The two awks are getting stuck because you have not given them any input from files or stdin. Maybe you meant to just do that in a BEGIN rule, not from real data.
Presumably you intend to use awk just to  get a couple of chars out of $var. There are shell options that will do substrings very nicely -- no need to run awk for that.
The awks won't work because you are using the shell construct for $awkvar. You should not use the $ inside awk itself (it designates a field number inside awk).
You have discovered the shell substring operator, but you don't show your updated script.
The new fail in if [ $begin = "-r" ] is because it expands $begin, and the test sees [ -r = -r ] and thinks -r is an option. If you use the better version of the test syntax and quote it, it will work.
paul $ begin="-r"
paul $ if [[ "${begin}" = "-r" ]]; then echo true; fi
true
paul $ 

