猿问

如何cat << EOF >>包含代码的文件?

如何cat << EOF >>包含代码的文件?

我想使用cat <<EOF >>以下代码将代码打印到文件中:

cat <<EOF >> brightup.sh!/bin/bash
curr=`cat /sys/class/backlight/intel_backlight/actual_brightness`if [ $curr -lt 4477 ]; then
   curr=$((curr+406));
   echo $curr  > /sys/class/backlight/intel_backlight/brightness;fiEOF

但是当我检查文件输出时,我得到了这个:

!/bin/bash
curr=1634if [  -lt 4477 ]; then
   curr=406;
   echo   > /sys/class/backlight/intel_backlight/brightness;fi

我尝试使用单引号,但输出也带有单引号。我该如何避免这个问题?


元芳怎么了
浏览 1990回答 3
3回答

MMMHUHU

这应该有用,我只是测试了它并且它按预期工作:没有扩展,替换,或者你发生了什么。cat&nbsp;<<<&nbsp;' #!/bin/bash curr=`cat&nbsp;/sys/class/backlight/intel_backlight/actual_brightness` if&nbsp;[&nbsp;$curr&nbsp;-lt&nbsp;4477&nbsp;];&nbsp;then &nbsp;&nbsp;curr=$((curr+406)); &nbsp;&nbsp;echo&nbsp;$curr&nbsp;&nbsp;>&nbsp;/sys/class/backlight/intel_backlight/brightness; fi'&nbsp;>&nbsp;file&nbsp;#&nbsp;use&nbsp;overwrite&nbsp;mode&nbsp;so&nbsp;that&nbsp;you&nbsp;don't&nbsp;keep&nbsp;on&nbsp;appending&nbsp;the&nbsp;same&nbsp;script&nbsp;to&nbsp;that&nbsp;file&nbsp;over&nbsp;and&nbsp;over&nbsp;again,&nbsp;unless&nbsp;that's&nbsp;what&nbsp;you&nbsp;want.使用以下也有效。cat&nbsp;<<<&nbsp;'&nbsp;>&nbsp;file &nbsp;...&nbsp;code&nbsp;...'此外,值得注意的是,当使用heredoc时,例如<< EOF,替换和可变扩展等发生。所以做这样的事情:cat&nbsp;<<&nbsp;EOF&nbsp;>&nbsp;file cd&nbsp;"$HOME"echo&nbsp;"$PWD"&nbsp;#&nbsp;echo&nbsp;the&nbsp;current&nbsp;pathEOF总是会导致变量$HOME和扩展$PWD。因此,如果您的主目录是/home/foobar当前路径/home/foobar/bin,file则如下所示:cd&nbsp;"/home/foobar"echo&nbsp;"/home/foobar/bin"而不是预期的:cd&nbsp;"$HOME"echo&nbsp;"$PWD"
随时随地看视频慕课网APP
我要回答