0

I am trying to output a text file which has MIPS code, however it keeps picking up the arguments and won't write them to the text file.

Code:

function errorMessage() {
echo "You have entered the wrong amount of arguments. The default files will now be used"
echo '#input.txt' > input.txt
"add $s0 $s1 $s2" >> input.txt
"sub $s2 $t0 $t3" >> input.txt
"add $s0 $s1 $z2" >> input.txt
"lw $t1 8($t2)" >> input.txt
"addi $t3 $s0 -9" >> input.txt
"sw $s3 4($t0)" >> input.txt
"lw $t11 70000($s0)'" >> input.txt

}

Results:

 #input.txt    
    add   
    sub   
    add   
    lw  8()    
    addi   -9    
    sw  4()    
    lw  70000()    
3
  • You should pass the arguments to the function.. What your function called in the code? Commented May 5, 2017 at 12:10
  • You need to escape special characters. Try echo "add \$s0 \$s1 \$s2" >> input.txt Commented May 5, 2017 at 12:13
  • use single quotes so they don't expand. You also either need echo on every line or just a single redirection > input.txt Commented May 5, 2017 at 12:16

2 Answers 2

1

One way of creating files by writing to them is by using whats called a Here Document.

#!/bin/bash
(
cat <<'EOF'
#input.txt
add $s0 $s1 $s2
sub $s2 $t0 $t3
add $s0 $s1 $z2
lw $t1 8($t2)
addi $t3 $s0 -9
sw $s3 4($t0)
lw $t11 70000($s0)
EOF
) > input.txt

What this does is it takes the text between cat <<'EOF' and EOF and writes it to input.txt. Because the EOF in cat <<'EOF' is quoted, the variables that have a $ before them are not treated as bash variables but rather real text. You can replace the string EOF for anything that you want. You can learn more about Here Documents here: http://tldp.org/LDP/abs/html/here-docs.html.

Sign up to request clarification or add additional context in comments.

Comments

1

If you mean for $s0 to be output literally, you need to use single-quotes rather than double quotes:

echo 'add $s0 $s1 $s2' >> input.txt

You can improve your script by writing once (using a here-document) rather than reopening input.txt for every line:

function errorMessage() {
echo >&2 "You have entered the wrong amount of arguments. The default files will now be used"
cat <<'END' >input.txt
#input.txt
add $s0 $s1 $s2
sub $s2 $t0 $t3
add $s0 $s1 $z2
lw $t1 8($t2)
addi $t3 $s0 -9
sw $s3 4($t0)
lw $t11 70000($s0)
END
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.