如何在Bash中引用参数

如何在Bash中引用参数,bash,Bash,我正在尝试输出一个包含MIPS代码的文本文件,但是它一直拾取参数,并且不会将它们写入文本文件 代码: 结果: #input.txt add sub add lw 8() addi -9 sw 4() lw 70000() 通过写入文件来创建文件的一种方法是使用所谓的Here文档 #!/bin/bash ( cat <<'EOF' #input.txt add

我正在尝试输出一个包含MIPS代码的文本文件,但是它一直拾取参数,并且不会将它们写入文本文件

代码:

结果:

 #input.txt    
    add   
    sub   
    add   
    lw  8()    
    addi   -9    
    sw  4()    
    lw  70000()    

通过写入文件来创建文件的一种方法是使用所谓的Here文档

#!/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
#/bin/bash
(

cat通过写入文件来创建文件的一种方法是使用所谓的Here文档

#!/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
!/bin/bash
(

cat如果您的意思是要按字面意思输出
$s0
,则需要使用单引号而不是双引号:

echo 'add $s0 $s1 $s2' >> input.txt
您可以通过编写一次(使用here文档)来改进脚本,而不是对每行重新打开
input.txt

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
}
函数errorMessage(){
echo>&2“您输入了错误数量的参数。现在将使用默认文件”

cat如果您的意思是要按字面意思输出
$s0
,则需要使用单引号而不是双引号:

echo 'add $s0 $s1 $s2' >> input.txt
您可以通过编写一次(使用here文档)来改进脚本,而不是对每行重新打开
input.txt

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
}
函数errorMessage(){
echo>&2“您输入了错误数量的参数。现在将使用默认文件”

cat您应该将参数传递给函数..您的函数在代码中调用了什么?您需要转义特殊字符。请尝试
echo“add\$s0\$s1\$s2”>>input.txt
使用单引号,这样它们就不会扩展。您还需要在每一行上使用echo,或者只需要一个重定向
>input.txt
您应该将参数传递给函数。您的函数在代码中调用了什么?您需要转义特殊字符。请尝试
echo“添加\$s0\$s1\$s2”>>input.txt
使用单引号,这样它们就不会展开。您还需要在每一行上使用echo,或者只使用一个重定向
>input.txt