简体   繁体   English

使用bash时如何保留所有格式(空格)?

[英]How do I keep all formatting (whitespace) when using bash?

I need to add 3 lines into a file via a bash script; 我需要通过bash脚本在文件中添加3行; sed and awk are not and can not be installed. sedawk不是也不能安装。 I have the following script insert the lines, but via the vertue of bash all formatting in the file is lost. 我有以下脚本插入行,但通过bash的vertue文件中的所有格式都将丢失。 I suspect its mangling the file in other ways as well as I get additional errors when I try to compile using the resulting, modified, file. 我怀疑它以其他方式破坏文件,当我尝试使用生成的,修改过的文件进行编译时,我会收到其他错误。

c=0
while read line
do
 if [ "$c" -eq 99 ]; then
  echo -e "// Added by build.sh\n#define PROGMEM\n#define prog_char\n\n$line"
 else
  echo "$line"
 fi
((c=c+1))
done < libdefs.h >> libdefs.h.new

Updated script: 更新的脚本:

This preserves the formatting, and adds the lines, but mangles the file somehow. 这样可以保留格式,并添加行,但会以某种方式破坏文件。 If I add the 3 lines manually and compile the app it works, if I use the script I get all kinds of errors. 如果我手动添加3行并编译它的应用程序,如果我使用该脚本,我会收到各种错误。

c=0
while IFS= read line
do
 if [ "$c" -eq 99 ]; then
  echo "// Added by build.sh"
  echo "#define PROGMEM"
  echo "#define prog_char"
  echo ""
 fi
 echo "$line"
 ((c=c+1))
done < libdefs.h >> libdefs.h.new

The first being libdefs.h:363:2: error: expected identifier or '(' before numeric constant . so it looks like parens are being messed with. 第一个是libdefs.h:363:2: error: expected identifier or '(' before numeric constant 。所以它看起来像parens被搞乱了。

Diff: DIFF:

--- libdefs.h.old   2011-10-01 13:46:34.000000000 -1000
+++ libdefs.h   2012-03-02 22:00:16.000000000 -1000
@@ -97,6 +97,10 @@
    #include <avr/interrupt.h>
 #endif

+// Added by build.sh
+#define PROGMEM
+#define prog_char
+
 // create a type for boolean
 typedef int8_t boolean;

@@ -351,35 +355,35 @@
 typedef uint16_t TIMER_MODES;

 // The set of PWM modes
-#define PWM_MODES ((TIMER_MODES)( \
-   BV(TIMER_MODE_PWM8_PHASE_CORRECT) | \
-   BV(TIMER_MODE_PWM9_PHASE_CORRECT) | \
-   BV(TIMER_MODE_PWM10_PHASE_CORRECT) | \
-   BV(TIMER_MODE_PWM8_FAST) | \
-   BV(TIMER_MODE_PWM9_FAST) | \
-   BV(TIMER_MODE_PWM10_FAST) | \
-   BV(TIMER_MODE_PWM_PHASE_FREQ_ICR) | \
-   BV(TIMER_MODE_PWM_PHASE_FREQ_OCR) | \
-   BV(TIMER_MODE_PWM_PHASE_CORRECT_ICR) | \
-   BV(TIMER_MODE_PWM_PHASE_CORRECT_OCR) | \
-   BV(TIMER_MODE_PWM_FAST_ICR) | \
-   BV(TIMER_MODE_PWM_FAST_OCR) \
+#define PWM_MODES ((TIMER_MODES)( 
+   BV(TIMER_MODE_PWM8_PHASE_CORRECT) | 
+   BV(TIMER_MODE_PWM9_PHASE_CORRECT) | 
+   BV(TIMER_MODE_PWM10_PHASE_CORRECT) | 
+   BV(TIMER_MODE_PWM8_FAST) | 
+   BV(TIMER_MODE_PWM9_FAST) | 
+   BV(TIMER_MODE_PWM10_FAST) | 
+   BV(TIMER_MODE_PWM_PHASE_FREQ_ICR) | 
+   BV(TIMER_MODE_PWM_PHASE_FREQ_OCR) | 
+   BV(TIMER_MODE_PWM_PHASE_CORRECT_ICR) | 
+   BV(TIMER_MODE_PWM_PHASE_CORRECT_OCR) | 
+   BV(TIMER_MODE_PWM_FAST_ICR) | 
+   BV(TIMER_MODE_PWM_FAST_OCR) 
 ))

 // The set of ICR modes
-#define ICR_MODES ((TIMER_MODES)( \
-   BV(TIMER_MODE_PWM_PHASE_FREQ_ICR) | \
-   BV(TIMER_MODE_PWM_PHASE_CORRECT_ICR) | \
-   BV(TIMER_MODE_CTC_ICR) | \
-   BV(TIMER_MODE_PWM_FAST_ICR)  \
+#define ICR_MODES ((TIMER_MODES)( 
+   BV(TIMER_MODE_PWM_PHASE_FREQ_ICR) | 
+   BV(TIMER_MODE_PWM_PHASE_CORRECT_ICR) | 
+   BV(TIMER_MODE_CTC_ICR) | 
+   BV(TIMER_MODE_PWM_FAST_ICR)  
 ))

 // The set of OCR modes
-#define OCR_MODES ((TIMER_MODES)( \
-   BV(TIMER_MODE_CTC_OCR) | \
-   BV(TIMER_MODE_PWM_PHASE_FREQ_OCR) | \
-   BV(TIMER_MODE_PWM_PHASE_CORRECT_OCR) | \
-   BV(TIMER_MODE_PWM_FAST_OCR)  \
+#define OCR_MODES ((TIMER_MODES)( 
+   BV(TIMER_MODE_CTC_OCR) | 
+   BV(TIMER_MODE_PWM_PHASE_FREQ_OCR) | 
+   BV(TIMER_MODE_PWM_PHASE_CORRECT_OCR) | 
+   BV(TIMER_MODE_PWM_FAST_OCR)  
 ))

 #define modeIsPWM(mode) ( ((TIMER_MODES)(BV(mode))) & PWM_MODES)
@@ -389,29 +393,29 @@
 #define TIMER_NO_MODES 0

 // Define bits if all modes are supported
-#define TIMER_ALL_MODES ((TIMER_MODES)( \
-   BV(TIMER_MODE_NORMAL)|\
-   BV(TIMER_MODE_PWM8_PHASE_CORRECT)|\
-   BV(TIMER_MODE_PWM9_PHASE_CORRECT)|\
-   BV(TIMER_MODE_PWM10_PHASE_CORRECT)|\
-   BV(TIMER_MODE_CTC_OCR)|\
-   BV(TIMER_MODE_PWM8_FAST)|\
-   BV(TIMER_MODE_PWM9_FAST)|\
-   BV(TIMER_MODE_PWM10_FAST)|\
-   BV(TIMER_MODE_PWM_PHASE_FREQ_ICR)|\
-   BV(TIMER_MODE_PWM_PHASE_FREQ_OCR)|\
-   BV(TIMER_MODE_PWM_PHASE_CORRECT_ICR)|\
-   BV(TIMER_MODE_PWM_PHASE_CORRECT_OCR)|\
-   BV(TIMER_MODE_CTC_ICR)|\
-   BV(TIMER_MODE_PWM_FAST_ICR)|\
-   BV(TIMER_MODE_PWM_FAST_OCR)\
+#define TIMER_ALL_MODES ((TIMER_MODES)( 
+   BV(TIMER_MODE_NORMAL)|
+   BV(TIMER_MODE_PWM8_PHASE_CORRECT)|
+   BV(TIMER_MODE_PWM9_PHASE_CORRECT)|
+   BV(TIMER_MODE_PWM10_PHASE_CORRECT)|
+   BV(TIMER_MODE_CTC_OCR)|
+   BV(TIMER_MODE_PWM8_FAST)|
+   BV(TIMER_MODE_PWM9_FAST)|
+   BV(TIMER_MODE_PWM10_FAST)|
+   BV(TIMER_MODE_PWM_PHASE_FREQ_ICR)|
+   BV(TIMER_MODE_PWM_PHASE_FREQ_OCR)|
+   BV(TIMER_MODE_PWM_PHASE_CORRECT_ICR)|
+   BV(TIMER_MODE_PWM_PHASE_CORRECT_OCR)|
+   BV(TIMER_MODE_CTC_ICR)|
+   BV(TIMER_MODE_PWM_FAST_ICR)|
+   BV(TIMER_MODE_PWM_FAST_OCR)
 ))

 // Define bits for less capable timers
-#define TIMER_3BIT_MODES (BV(TIMER_MODE_NORMAL)| BV(TIMER_MODE_PWM8_PHASE_CORRECT)|BV(TIMER_MODE_CTC_OCR)| \
+#define TIMER_3BIT_MODES (BV(TIMER_MODE_NORMAL)| BV(TIMER_MODE_PWM8_PHASE_CORRECT)|BV(TIMER_MODE_CTC_OCR)| 
        BV(TIMER_MODE_PWM8_FAST)|BV(TIMER_MODE_PWM_PHASE_CORRECT_OCR)|BV(TIMER_MODE_PWM_FAST_OCR))

-#define TIMER_2BIT_MODES (BV(TIMER_MODE_NORMAL)| BV(TIMER_MODE_PWM8_PHASE_CORRECT)|BV(TIMER_MODE_CTC_OCR)| \
+#define TIMER_2BIT_MODES (BV(TIMER_MODE_NORMAL)| BV(TIMER_MODE_PWM8_PHASE_CORRECT)|BV(TIMER_MODE_CTC_OCR)| 
        BV(TIMER_MODE_PWM8_FAST))


@@ -444,11 +448,11 @@


 #define MAKE_TIMER_COMPARE_DATA() {0,null/*,0*/}
-#define MAKE_TIMER_COMPARE(data,timer,port,mask,threshold, intport,intmask, comport,combit,pwm) \
-   {&data,timer, {_SFR_MEM_ADDR(port),BV(mask)},_SFR_MEM_ADDR(threshold), \
-   {_SFR_MEM_ADDR(intport),BV(intmask)}, \
-   {_SFR_MEM_ADDR(comport),combit}, \
-   pwm \
+#define MAKE_TIMER_COMPARE(data,timer,port,mask,threshold, intport,intmask, comport,combit,pwm) 
+   {&data,timer, {_SFR_MEM_ADDR(port),BV(mask)},_SFR_MEM_ADDR(threshold), 
+   {_SFR_MEM_ADDR(intport),BV(intmask)}, 
+   {_SFR_MEM_ADDR(comport),combit}, 
+   pwm 
    }

 /**
@@ -493,27 +497,27 @@
 } TimerData;

 #define MAKE_TIMER_DATA(prescale) {prescale,/*0,*/null,null,null,null,TIMER_MODE_NORMAL}
-#define MAKE_TIMER(data, counter,prescaler,sixteenBit,rtc,modes, \
-   wgm0port, wgm0mask, wgm1port, wgm1mask, wgm2port, wgm2mask, wgm3port, wgm3mask, \
-    compares,ovrintport,ovrintmask,ovrreqport,ovrreqmask,icr, \
-    capintport,capintmask,capreqport,capreqmask, capedgeport,capedgemask,\
-    incapin \
-   ) \
-   {&data, _SFR_MEM_ADDR(counter), _SFR_MEM_ADDR(prescaler), sixteenBit, rtc, sizeof(compares)/sizeof(TimerCompare),modes, \
-       { \
-           {_SFR_MEM_ADDR(wgm0port),BV(wgm0mask)}, \
-           {_SFR_MEM_ADDR(wgm1port),BV(wgm1mask)}, \
-           {_SFR_MEM_ADDR(wgm2port),BV(wgm2mask)}, \
-           {_SFR_MEM_ADDR(wgm3port),BV(wgm3mask)}  \
-       },\
-       compares,\
-   {_SFR_MEM_ADDR(ovrintport),BV(ovrintmask)},\
-   {_SFR_MEM_ADDR(ovrreqport),BV(ovrreqmask)}, \
-   _SFR_MEM_ADDR(icr),\
-   {_SFR_MEM_ADDR(capintport),BV(capintmask)},\
-   {_SFR_MEM_ADDR(capreqport),BV(capreqmask)}, \
-   {_SFR_MEM_ADDR(capedgeport),BV(capedgemask)}, \
-   incapin \
+#define MAKE_TIMER(data, counter,prescaler,sixteenBit,rtc,modes, 
+   wgm0port, wgm0mask, wgm1port, wgm1mask, wgm2port, wgm2mask, wgm3port, wgm3mask, 
+    compares,ovrintport,ovrintmask,ovrreqport,ovrreqmask,icr, 
+    capintport,capintmask,capreqport,capreqmask, capedgeport,capedgemask,
+    incapin 
+   ) 
+   {&data, _SFR_MEM_ADDR(counter), _SFR_MEM_ADDR(prescaler), sixteenBit, rtc, sizeof(compares)/sizeof(TimerCompare),modes, 
+       { 
+           {_SFR_MEM_ADDR(wgm0port),BV(wgm0mask)}, 
+           {_SFR_MEM_ADDR(wgm1port),BV(wgm1mask)}, 
+           {_SFR_MEM_ADDR(wgm2port),BV(wgm2mask)}, 
+           {_SFR_MEM_ADDR(wgm3port),BV(wgm3mask)}  
+       },
+       compares,
+   {_SFR_MEM_ADDR(ovrintport),BV(ovrintmask)},
+   {_SFR_MEM_ADDR(ovrreqport),BV(ovrreqmask)}, 
+   _SFR_MEM_ADDR(icr),
+   {_SFR_MEM_ADDR(capintport),BV(capintmask)},
+   {_SFR_MEM_ADDR(capreqport),BV(capreqmask)}, 
+   {_SFR_MEM_ADDR(capedgeport),BV(capedgemask)}, 
+   incapin 
    }

 // Define the signature of code that is the destination of an rprintf output

You need to tell read to not split words (or rather, make it unable to split words). 你需要告诉read不要拆分单词(或者更确切地说,让它无法拆分单词)。

while IFS= read line

Also, echo -e on $line is going to do bad things. 另外, $line上的echo -e会做坏事。 Always print $line , and conditionally print the other stuff. 始终打印$line ,并有条件地打印其他内容。

EDIT: 编辑:

Aha. 啊哈。 You'll also need to use read -r to prevent it from killing your backslashes. 您还需要使用read -r来防止它杀死反斜杠。

This might work for you: 这可能对你有用:

OIFS=$IFS; IFS=$'\n'; a=($(<libdefs.h)); IFS=$OIFS
a[98]=$'// Added by build.sh\n#define PROGMEM\n#define prog_char\n'${a[98]} 
printf "%s\n" "${a[@]}" >libdefs.h.new

or this: 或这个:

{ head -n98 libdefs.h;
printf "// Added by build.sh\n#define PROGMEM\n#define prog_char\n"
tail -n+99 libdefs.h; } >libdefs.h.new

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 bash 的 subshel​​l 输出中修剪空格,而不是换行符? - How do I trim whitespace, but not newlines, from subshell output in bash? Bash:如何保持N个程序运行,如果其中一个失败,则重新启动所有程序? - Bash: How do I keep N programs running, restarting all if one fails? 将Bash子外壳程序的输出返回到OSX“默认”实用程序时,如何保持单引号完整? - How do I keep single quotes intact when returning the output of a Bash subshell to the OSX “defaults” utility? Bash:如何在使用剪切之前修剪空白 - Bash: How to trim whitespace before using cut 如何使用 Bash 脚本保持所有 GPU 设备运行任务? - How to keep all GPU devices running tasks using Bash script? bash正在用空间代替结肠,如何保持结肠? - bash is replacing colon with space, how do I keep the colons? Bash regexp:如何用字母替换所有非空白字符? - Bash regexp: How to replace all non-whitespace characters with a letter? 如何防止空格出现在这些bash变量中? - How do prevent whitespace from appearing in these bash variables? 如何使用 bash 从字符串中提取所有 ip 地址 - How do I extract all the ip addresses from the string using bash 如何使用带有stdin的wall命令向python中的所有bash终端广播消息? - How do I broadcast messages to all bash terminal in python using wall command with stdin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM