My shell script is giving me the wrong output

0

I have created a script that is supposed to (in the below section) store the name and location of their corresponding values in an assigned variable, then list the name variables in a column. I have successfully done this before, but for some reason all I am getting are empty lines. Please help me because I can't seem to make heads or tails of this problem.

#This is where sections are added or removed
#SECTIONnumberNAME=$(echo "number: section name")
#SECTIONnumberLOCARION=./Setup_Fedora_Sections/section_name.sh

SECTION1NAME=$(echo "1: Wireless Card")
SECTION1LOCARION=./Setup_Fedora_Sections/Setup_Fedora_Wireless_Card.sh
SECTION2NAME=$(echo "2: Basic Setup")
SECTION2LOCARION=./Setup_Fedora_Sections/Setup_Fedora_Basic_Setup.sh
SECTION3NAME=$(echo "3: Optional Programs")
SECTION3LOCARION=./Setup_Fedora_Sections/Setup_Fedora_Optional_Programs.sh

#This is where selection offers are added or removed
#echo $'\n'$SELECTIONnumberNAME
echo $'\n'$SELECTION1NAME
echo $'\n'$SELECTION2NAME
echo $'\n'$SELECTION3NAME

Purpe_Fedora

Posted 2016-06-28T21:30:25.603

Reputation: 67

Why would you ever use variable=$(echo text) instead of variable=text? – AFH – 2016-06-28T21:56:54.860

Often it is cosy to copy and past always (or never) the variable name to avoid to make errors... You are printing empty variables ($SELECTIONxNAME and not $SECTIONxNAME). Moreover to debug a script you can write set - (and set +) at the beginning (and the end) of the part to debug. – Hastur – 2016-06-28T22:01:42.990

Answers

1

You may want to try changing the text "SELECTION" in the later part of the script to be "SECTION" to match the first part of the script. It looks like a simple spelling issue. So, this works for me:

#This is where selection offers are added or removed
#echo $'\n'$SELECTIONnumberNAME
echo $'\n'$SECTION1NAME
echo $'\n'$SECTION2NAME
echo $'\n'$SECTION3NAME

DKing

Posted 2016-06-28T21:30:25.603

Reputation: 250

Wow thanks it so easy to miss those differences, thanks for pointing my oversight out. – Purpe_Fedora – 2016-06-29T01:20:15.150

just out of curiosity is there a way to make the above correction into a variable and have it display the same way and not on one line? – Purpe_Fedora – 2016-06-29T01:43:01.867

There are probably several changes I would personally make if that were my code. First, I would go with AFH's recommendation and simply use variable="value". But I think that what you are looking for would be an array. Also, when doing the echo, I wouldn't do $'\n', but instead would do something like echo -e "\n$variable1\n$variable2\n". – DKing – 2016-06-29T13:51:02.813

whats the difference between the two methods? – Purpe_Fedora – 2016-07-01T16:28:29.443

The first one is more obvious. Simply assigning text to a variable directly is easy, and instant. Trying to create a subshell and run a separate command and then piping that output into the variable takes resources, and it's longer to write. As for the last part, it's just convenience and preference. Rather than having to have multiple commands, you can have one, and you only need a single string. It's also fewer characters to type. – DKing – 2016-07-01T18:12:36.060