expansion https://www.e-learn.cn/tag/expansion zh-hans Variable not recognized in else statement in a bat file https://www.e-learn.cn/topic/4100984 <span>Variable not recognized in else statement in a bat file</span> <span><span lang="" about="/user/26" typeof="schema:Person" property="schema:name" datatype="">*爱你&永不变心*</span></span> <span>2021-02-11 17:14:49</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have the follow code:</p> <pre><code>ECHO OFF :LOOP set c= setlocal ENABLEDELAYEDEXPANSION tasklist | find /i "calc.exe" &gt;nul 2&gt;&amp;1 IF %ERRORLEVEL% == 0 ( ECHO easymeetingOnCall is running taskkill /IM calc.exe /F set c=true Timeout /T 10 goto LOOP ) ELSE ( IF "!c!" == "true" ( ECHO l'applicazione easymeetingOnCall è spenta. Timeout /T 10 goto exitLoop ) ECHO easymeetingOnCall is not running Timeout /T 5 /Nobreak GOTO LOOP ) :exitLoop </code></pre> <p>My problem is that the follow condition</p> <pre><code>IF "!c!" == "true" </code></pre> <p>Into the else statement, is not recognized. If I write <code>echo !c!</code> It doesn't output any result. I <code>set enabledelayedexpansion</code> before, so I don't know why of this behavior.</p> <p>Could you help me? Thanks to all.</p> <br /><h3>回答1:</h3><br /><p>Look carefully at the following extraction from your code.</p> <pre><code>ECHO OFF :LOOP set c= .... set c=true goto :LOOP </code></pre> <p>You are explicitly telling the if statement to <code>goto :loop</code> where directly after you <code>set c=</code> which now gives it no value. Move the <code>set c=</code> to above to label to retain the value you've set.</p> <p>I however suggest a few changes. you can get away without <code>delayedexpansion</code> and do actually require all the for loops. Using <code>taskkill</code> to explicitly search for the app is better than listing all, also already mentioned to you in a comment by @Mofi. Lastly, it is not really good practice to set single character variables, though it does not always cause issues, I would suggest using multple character variable names. I just changed your variable <code>%c%</code> to <code>%_c%</code></p> <pre><code>@echo off &amp; set _c= :loop tasklist /FI "IMAGENAME eq Calc.exe" | find /i "Calc" goto :_%errorlevel% :_0 ECHO easymeetingOnCall is running taskkill /IM calc.exe /F set _c=true Timeout /T 10 goto loop :_1 IF "%_c%" == "true" ( ECHO l'applicazione easymeetingOnCall è spenta. Timeout /T 10 goto exitloop ) ECHO easymeetingOnCall is not running Timeout /T 5 /Nobreak goto loop :exitloop </code></pre> <p><strong>Edit</strong>, as you wanted to do this with a list:</p> <pre><code>@echo off &amp; set _c= set "list=calc.exe StickyNot.exe wordpad.exe" for %%a in (%list%) do call :loop %%a goto :eof :loop set task=%1 tasklist /FI "IMAGENAME eq %1" | find /i "%1" if not errorlevel 1 ( ECHO easymeetingOnCall is running %1 taskkill /IM %1 /F set _c=true Timeout /T 10 ) if "%_c%" == "true" ( ECHO l'applicazione easymeetingOnCall è spenta. %1 Timeout /T 10 goto exitloop ) echo easymeetingOnCall is not running %1 Timeout /T 5 /Nobreak goto :loop :exitloop </code></pre> <p>Be aware, if the first process is not running, it will loop forever until it is found, then only kill it and go to the next process. If you do not want that, then change <code>goto :loop</code> in the last line to exit loop.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/63357127/variable-not-recognized-in-else-statement-in-a-bat-file</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/windows" hreflang="zh-hans">windows</a></div> <div class="field--item"><a href="/tag/batch-file" hreflang="zh-hans">batch-file</a></div> <div class="field--item"><a href="/tag/cmd" hreflang="zh-hans">cmd</a></div> <div class="field--item"><a href="/tag/scope" hreflang="zh-hans">scope</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> </div> </div> Thu, 11 Feb 2021 09:14:49 +0000 *爱你&永不变心* 4100984 at https://www.e-learn.cn Variable not recognized in else statement in a bat file https://www.e-learn.cn/topic/4100960 <span>Variable not recognized in else statement in a bat file</span> <span><span lang="" about="/user/175" typeof="schema:Person" property="schema:name" datatype="">一笑奈何</span></span> <span>2021-02-11 17:11:32</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I have the follow code:</p> <pre><code>ECHO OFF :LOOP set c= setlocal ENABLEDELAYEDEXPANSION tasklist | find /i "calc.exe" &gt;nul 2&gt;&amp;1 IF %ERRORLEVEL% == 0 ( ECHO easymeetingOnCall is running taskkill /IM calc.exe /F set c=true Timeout /T 10 goto LOOP ) ELSE ( IF "!c!" == "true" ( ECHO l'applicazione easymeetingOnCall è spenta. Timeout /T 10 goto exitLoop ) ECHO easymeetingOnCall is not running Timeout /T 5 /Nobreak GOTO LOOP ) :exitLoop </code></pre> <p>My problem is that the follow condition</p> <pre><code>IF "!c!" == "true" </code></pre> <p>Into the else statement, is not recognized. If I write <code>echo !c!</code> It doesn't output any result. I <code>set enabledelayedexpansion</code> before, so I don't know why of this behavior.</p> <p>Could you help me? Thanks to all.</p> <br /><h3>回答1:</h3><br /><p>Look carefully at the following extraction from your code.</p> <pre><code>ECHO OFF :LOOP set c= .... set c=true goto :LOOP </code></pre> <p>You are explicitly telling the if statement to <code>goto :loop</code> where directly after you <code>set c=</code> which now gives it no value. Move the <code>set c=</code> to above to label to retain the value you've set.</p> <p>I however suggest a few changes. you can get away without <code>delayedexpansion</code> and do actually require all the for loops. Using <code>taskkill</code> to explicitly search for the app is better than listing all, also already mentioned to you in a comment by @Mofi. Lastly, it is not really good practice to set single character variables, though it does not always cause issues, I would suggest using multple character variable names. I just changed your variable <code>%c%</code> to <code>%_c%</code></p> <pre><code>@echo off &amp; set _c= :loop tasklist /FI "IMAGENAME eq Calc.exe" | find /i "Calc" goto :_%errorlevel% :_0 ECHO easymeetingOnCall is running taskkill /IM calc.exe /F set _c=true Timeout /T 10 goto loop :_1 IF "%_c%" == "true" ( ECHO l'applicazione easymeetingOnCall è spenta. Timeout /T 10 goto exitloop ) ECHO easymeetingOnCall is not running Timeout /T 5 /Nobreak goto loop :exitloop </code></pre> <p><strong>Edit</strong>, as you wanted to do this with a list:</p> <pre><code>@echo off &amp; set _c= set "list=calc.exe StickyNot.exe wordpad.exe" for %%a in (%list%) do call :loop %%a goto :eof :loop set task=%1 tasklist /FI "IMAGENAME eq %1" | find /i "%1" if not errorlevel 1 ( ECHO easymeetingOnCall is running %1 taskkill /IM %1 /F set _c=true Timeout /T 10 ) if "%_c%" == "true" ( ECHO l'applicazione easymeetingOnCall è spenta. %1 Timeout /T 10 goto exitloop ) echo easymeetingOnCall is not running %1 Timeout /T 5 /Nobreak goto :loop :exitloop </code></pre> <p>Be aware, if the first process is not running, it will loop forever until it is found, then only kill it and go to the next process. If you do not want that, then change <code>goto :loop</code> in the last line to exit loop.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/63357127/variable-not-recognized-in-else-statement-in-a-bat-file</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/windows" hreflang="zh-hans">windows</a></div> <div class="field--item"><a href="/tag/batch-file" hreflang="zh-hans">batch-file</a></div> <div class="field--item"><a href="/tag/cmd" hreflang="zh-hans">cmd</a></div> <div class="field--item"><a href="/tag/scope" hreflang="zh-hans">scope</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> </div> </div> Thu, 11 Feb 2021 09:11:32 +0000 一笑奈何 4100960 at https://www.e-learn.cn Do we need to call wordfree upon wordexp failure? https://www.e-learn.cn/topic/4081634 <span>Do we need to call wordfree upon wordexp failure?</span> <span><span lang="" about="/user/121" typeof="schema:Person" property="schema:name" datatype="">对着背影说爱祢</span></span> <span>2021-02-08 19:38:27</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>Do we need to call wordfree upon wordexp failure? Calling wordfree seems to segfault in some cases (eg when wordfree returns error code with string is "foo 'bar"). This isn't clear from man page, and I've seen wordfree used in some error cases.</p> <br /><h3>回答1:</h3><br /><p>According to the GNU's manual example, it should be called on error only if <code>WRDE_NOSPACE</code> was returned:</p> <pre><code>switch (wordexp (program, &amp;result, 0)) { case 0: /* Successful. */ break; case WRDE_NOSPACE: /* If the error was WRDE_NOSPACE, then perhaps part of the result was allocated. */ wordfree (&amp;result); default: /* Some other error. */ return -1; } </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/23120084/do-we-need-to-call-wordfree-upon-wordexp-failure</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/c-1" hreflang="zh-hans">c</a></div> <div class="field--item"><a href="/tag/posix" hreflang="zh-hans">posix</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> </div> </div> Mon, 08 Feb 2021 11:38:27 +0000 对着背影说爱祢 4081634 at https://www.e-learn.cn Macro stepper in DrRacket https://www.e-learn.cn/topic/4014759 <span>Macro stepper in DrRacket</span> <span><span lang="" about="/user/242" typeof="schema:Person" property="schema:name" datatype="">拟墨画扇</span></span> <span>2021-01-27 08:00:49</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>On the link http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html there are instructions for working with the macro stepper.</p> <p>However, when I'm going to try it, I can't get the second expansion of myor in the definition of nonzero? function, only the first. Also, I have no buttons "Previous term" and "Next term".</p> <p>So my question is: how i must configure macro stepper to get the second expansion, like in tutorial?</p> <br /><h3>回答1:</h3><br /><p>I'm assuming that your source program looked something like this:</p> <pre><code>#lang racket (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <p>Short story: use <strong>syntax-case</strong> rather than <strong>syntax-rules</strong> for the moment; there appears to be some bug associated with the Macro stepper and syntax-rules. I've sent along a bug report to the Racket developers, so hopefully this will be fixed soon. The <strong>syntax-case</strong> version of the above program looks like this.</p> <pre><code>#lang racket (define-syntax (myor stx) (syntax-case stx () [(_ e) #'e] [(_ e1 . es) #'(let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <hr /><p>Below is the longer story...</p> <p>When I run your program under the pre-release of 5.2.1, I see the following in the Macro Stepper, with Macro hiding set to "Standard":</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (let:26 ([r:26 (negative? r)]) (if:26 r:26 r:26 (myor:26 (positive? r))))))) </code></pre> <p>which looks wrong. It expands only one use of <strong>myor</strong> out to uses of <strong>if</strong>. Very odd!</p> <p>Let's see what things look like under Racket 5.2...</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (let ([r (negative? r)]) (if r r (myor (positive? r))))))) </code></pre> <p>Ah. Ok, I can confirm that I see the same problem that you see in Racket 5.2, as well as the pre-release.</p> <p>The bug seems related to the behavior of the "Macro hiding" feature, which tries to not overwhelm you with the full expansion when it is set to Standard. If you set it to "Disabled", you'll see that the macro debugger will show the expansion in its full, unvarnished glory, and it does include the expansion that we expect to see:</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntaxes (myor) (lambda (x) ; ... I'm omitting the content here: it's way too long. )) (define-values:20 (nonzero?) (lambda:21 (r) (let-values:22 (((r) (#%app:23 negative? r))) (if r r (#%app:24 positive? r))))))) </code></pre> <p>I'll write a bug report and send it to the Racket developers. </p> <p>If you write your macro using <strong>syntax-case</strong>, as opposed to <strong>syntax-rules</strong>, it appears to work better with the Macro Stepper.</p> <pre><code>#lang racket (define-syntax (myor stx) (syntax-case stx () [(_ e) #'e] [(_ e1 . es) #'(let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <p>When I step through this, it appears to work a lot better. So whatever is triggering the bug, it appears to be some interaction with the Macro Stepper and <strong>syntax-rules</strong>. So try using <strong>syntax-case</strong> instead.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>It worked for me without having to do anything additional. Try with the newest version of Racket, also try choosing a different language from the menu, and make sure that <code>debugging</code> is selected for the language you select.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/8961762/macro-stepper-in-drracket</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/macros" hreflang="zh-hans">macros</a></div> <div class="field--item"><a href="/tag/scheme" hreflang="zh-hans">scheme</a></div> <div class="field--item"><a href="/tag/racket" hreflang="zh-hans">racket</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> </div> </div> Wed, 27 Jan 2021 00:00:49 +0000 拟墨画扇 4014759 at https://www.e-learn.cn Macro stepper in DrRacket https://www.e-learn.cn/topic/4014697 <span>Macro stepper in DrRacket</span> <span><span lang="" about="/user/175" typeof="schema:Person" property="schema:name" datatype="">一笑奈何</span></span> <span>2021-01-27 07:54:50</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>On the link http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html there are instructions for working with the macro stepper.</p> <p>However, when I'm going to try it, I can't get the second expansion of myor in the definition of nonzero? function, only the first. Also, I have no buttons "Previous term" and "Next term".</p> <p>So my question is: how i must configure macro stepper to get the second expansion, like in tutorial?</p> <br /><h3>回答1:</h3><br /><p>I'm assuming that your source program looked something like this:</p> <pre><code>#lang racket (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <p>Short story: use <strong>syntax-case</strong> rather than <strong>syntax-rules</strong> for the moment; there appears to be some bug associated with the Macro stepper and syntax-rules. I've sent along a bug report to the Racket developers, so hopefully this will be fixed soon. The <strong>syntax-case</strong> version of the above program looks like this.</p> <pre><code>#lang racket (define-syntax (myor stx) (syntax-case stx () [(_ e) #'e] [(_ e1 . es) #'(let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <hr /><p>Below is the longer story...</p> <p>When I run your program under the pre-release of 5.2.1, I see the following in the Macro Stepper, with Macro hiding set to "Standard":</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (let:26 ([r:26 (negative? r)]) (if:26 r:26 r:26 (myor:26 (positive? r))))))) </code></pre> <p>which looks wrong. It expands only one use of <strong>myor</strong> out to uses of <strong>if</strong>. Very odd!</p> <p>Let's see what things look like under Racket 5.2...</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (let ([r (negative? r)]) (if r r (myor (positive? r))))))) </code></pre> <p>Ah. Ok, I can confirm that I see the same problem that you see in Racket 5.2, as well as the pre-release.</p> <p>The bug seems related to the behavior of the "Macro hiding" feature, which tries to not overwhelm you with the full expansion when it is set to Standard. If you set it to "Disabled", you'll see that the macro debugger will show the expansion in its full, unvarnished glory, and it does include the expansion that we expect to see:</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntaxes (myor) (lambda (x) ; ... I'm omitting the content here: it's way too long. )) (define-values:20 (nonzero?) (lambda:21 (r) (let-values:22 (((r) (#%app:23 negative? r))) (if r r (#%app:24 positive? r))))))) </code></pre> <p>I'll write a bug report and send it to the Racket developers. </p> <p>If you write your macro using <strong>syntax-case</strong>, as opposed to <strong>syntax-rules</strong>, it appears to work better with the Macro Stepper.</p> <pre><code>#lang racket (define-syntax (myor stx) (syntax-case stx () [(_ e) #'e] [(_ e1 . es) #'(let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <p>When I step through this, it appears to work a lot better. So whatever is triggering the bug, it appears to be some interaction with the Macro Stepper and <strong>syntax-rules</strong>. So try using <strong>syntax-case</strong> instead.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>It worked for me without having to do anything additional. Try with the newest version of Racket, also try choosing a different language from the menu, and make sure that <code>debugging</code> is selected for the language you select.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/8961762/macro-stepper-in-drracket</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/macros" hreflang="zh-hans">macros</a></div> <div class="field--item"><a href="/tag/scheme" hreflang="zh-hans">scheme</a></div> <div class="field--item"><a href="/tag/racket" hreflang="zh-hans">racket</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> </div> </div> Tue, 26 Jan 2021 23:54:50 +0000 一笑奈何 4014697 at https://www.e-learn.cn Macro stepper in DrRacket https://www.e-learn.cn/topic/4014668 <span>Macro stepper in DrRacket</span> <span><span lang="" about="/user/12" typeof="schema:Person" property="schema:name" datatype="">倖福魔咒の</span></span> <span>2021-01-27 07:52:50</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>On the link http://www.ccs.neu.edu/home/ryanc/macro-stepper/tutorial.html there are instructions for working with the macro stepper.</p> <p>However, when I'm going to try it, I can't get the second expansion of myor in the definition of nonzero? function, only the first. Also, I have no buttons "Previous term" and "Next term".</p> <p>So my question is: how i must configure macro stepper to get the second expansion, like in tutorial?</p> <br /><h3>回答1:</h3><br /><p>I'm assuming that your source program looked something like this:</p> <pre><code>#lang racket (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <p>Short story: use <strong>syntax-case</strong> rather than <strong>syntax-rules</strong> for the moment; there appears to be some bug associated with the Macro stepper and syntax-rules. I've sent along a bug report to the Racket developers, so hopefully this will be fixed soon. The <strong>syntax-case</strong> version of the above program looks like this.</p> <pre><code>#lang racket (define-syntax (myor stx) (syntax-case stx () [(_ e) #'e] [(_ e1 . es) #'(let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <hr /><p>Below is the longer story...</p> <p>When I run your program under the pre-release of 5.2.1, I see the following in the Macro Stepper, with Macro hiding set to "Standard":</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (let:26 ([r:26 (negative? r)]) (if:26 r:26 r:26 (myor:26 (positive? r))))))) </code></pre> <p>which looks wrong. It expands only one use of <strong>myor</strong> out to uses of <strong>if</strong>. Very odd!</p> <p>Let's see what things look like under Racket 5.2...</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntax myor (syntax-rules () [(myor e) e] [(myor e1 . es) (let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (let ([r (negative? r)]) (if r r (myor (positive? r))))))) </code></pre> <p>Ah. Ok, I can confirm that I see the same problem that you see in Racket 5.2, as well as the pre-release.</p> <p>The bug seems related to the behavior of the "Macro hiding" feature, which tries to not overwhelm you with the full expansion when it is set to Standard. If you set it to "Disabled", you'll see that the macro debugger will show the expansion in its full, unvarnished glory, and it does include the expansion that we expect to see:</p> <pre><code>(module anonymous-module racket (#%module-begin (define-syntaxes (myor) (lambda (x) ; ... I'm omitting the content here: it's way too long. )) (define-values:20 (nonzero?) (lambda:21 (r) (let-values:22 (((r) (#%app:23 negative? r))) (if r r (#%app:24 positive? r))))))) </code></pre> <p>I'll write a bug report and send it to the Racket developers. </p> <p>If you write your macro using <strong>syntax-case</strong>, as opposed to <strong>syntax-rules</strong>, it appears to work better with the Macro Stepper.</p> <pre><code>#lang racket (define-syntax (myor stx) (syntax-case stx () [(_ e) #'e] [(_ e1 . es) #'(let ([r e1]) (if r r (myor . es)))])) (define (nonzero? r) (myor (negative? r) (positive? r))) </code></pre> <p>When I step through this, it appears to work a lot better. So whatever is triggering the bug, it appears to be some interaction with the Macro Stepper and <strong>syntax-rules</strong>. So try using <strong>syntax-case</strong> instead.</p> <br /><br /><br /><h3>回答2:</h3><br /><p>It worked for me without having to do anything additional. Try with the newest version of Racket, also try choosing a different language from the menu, and make sure that <code>debugging</code> is selected for the language you select.</p> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/8961762/macro-stepper-in-drracket</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/macros" hreflang="zh-hans">macros</a></div> <div class="field--item"><a href="/tag/scheme" hreflang="zh-hans">scheme</a></div> <div class="field--item"><a href="/tag/racket" hreflang="zh-hans">racket</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> </div> </div> Tue, 26 Jan 2021 23:52:50 +0000 倖福魔咒の 4014668 at https://www.e-learn.cn Replacing 'source file' with its content, and expanding variables, in bash https://www.e-learn.cn/topic/3956583 <span>Replacing &#039;source file&#039; with its content, and expanding variables, in bash</span> <span><span lang="" about="/user/114" typeof="schema:Person" property="schema:name" datatype="">﹥>﹥吖頭↗</span></span> <span>2020-12-08 07:55:53</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>In a script.sh,</p> <pre><code>source a.sh source b.sh CMD1 CMD2 CMD3 </code></pre> <p>how can I replace the <code>source *.sh</code> with their content (without executing the commands)? I would like to see what the bash interpreter executes after sourcing the files and expanding all variables.</p> <p>I know I can use <code>set -n -v</code> or run <code>bash -n -v script.sh 2&gt;output.sh</code>, but that would not replace the source commands (and even less if a.sh or b.sh contain variables).</p> <p>I thought of using a subshell, but that still doesn't expand the source lines. I tried a combination of <code>set +n +v</code> and <code>set -n -v</code> before and after the source lines, but that still does not work.</p> <p>I'm going to send that output to a remote machine using ssh. I could use <code>&lt;&lt;output.sh</code> to pipe the content into the ssh command, but I can't log as root onto the remote machine, but I am however a sudoer. Therefore, I thought I could create the script and send it as a base64-encoded string (using that clever trick ) <code>base64 script | ssh remotehost 'base64 -d | sudo bash'</code></p> <p>Is there a solution? Or do you have a better idea?</p> <br /><h3>回答1:</h3><br /><p>You can do something like this:</p> <p>inline.sh:</p> <pre><code>#!/usr/bin/env bash while read line; do if [[ "$line" =~ (\.|source)\s+.+ ]]; then file="$(echo $line | cut -d' ' -f2)" echo "$(cat $file)" else echo "$line" fi done &lt; "$1" </code></pre> <p>Note this assumes the <code>source</code>d files exist, and doesn't handle errors. You should also handle possible hashbangs. If the <code>sourced</code> files contain themselves <code>source</code>, you need to apply the script recursively, e.g. something like (not tested):</p> <pre><code>while egrep -q '^(source|\.)' main.sh; do bash inline.sh main.sh &gt; main.sh done </code></pre> <p>Let's test it</p> <p>main.sh:</p> <pre><code>source a.sh . b.sh echo cc echo "$var_a $var_b" </code></pre> <p>a.sh:</p> <pre><code>echo aa var_a="stack" </code></pre> <p>b.sh:</p> <pre><code>echo bb var_b="overflow" </code></pre> <p>Result:</p> <pre><code>bash inline.sh main.sh echo aa var_a="stack" echo bb var_b="overflow" echo cc echo "$var_a $var_b" bash inline.sh main.sh | bash aa bb cc stack overflow </code></pre> <p>BTW, if you just want to see what bash executes, you can run</p> <pre><code>bash -x [script] </code></pre> <p>or remotely</p> <pre><code>ssh user@host -t "bash -x [script]" </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/37531927/replacing-source-file-with-its-content-and-expanding-variables-in-bash</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/bash" hreflang="zh-hans">bash</a></div> <div class="field--item"><a href="/tag/set" hreflang="zh-hans">set</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> <div class="field--item"><a href="/tag/subshell" hreflang="zh-hans">subshell</a></div> </div> </div> Mon, 07 Dec 2020 23:55:53 +0000 ﹥>﹥吖頭↗ 3956583 at https://www.e-learn.cn Replacing 'source file' with its content, and expanding variables, in bash https://www.e-learn.cn/topic/3956580 <span>Replacing &#039;source file&#039; with its content, and expanding variables, in bash</span> <span><span lang="" about="/user/100" typeof="schema:Person" property="schema:name" datatype="">点点圈</span></span> <span>2020-12-08 07:55:09</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>In a script.sh,</p> <pre><code>source a.sh source b.sh CMD1 CMD2 CMD3 </code></pre> <p>how can I replace the <code>source *.sh</code> with their content (without executing the commands)? I would like to see what the bash interpreter executes after sourcing the files and expanding all variables.</p> <p>I know I can use <code>set -n -v</code> or run <code>bash -n -v script.sh 2&gt;output.sh</code>, but that would not replace the source commands (and even less if a.sh or b.sh contain variables).</p> <p>I thought of using a subshell, but that still doesn't expand the source lines. I tried a combination of <code>set +n +v</code> and <code>set -n -v</code> before and after the source lines, but that still does not work.</p> <p>I'm going to send that output to a remote machine using ssh. I could use <code>&lt;&lt;output.sh</code> to pipe the content into the ssh command, but I can't log as root onto the remote machine, but I am however a sudoer. Therefore, I thought I could create the script and send it as a base64-encoded string (using that clever trick ) <code>base64 script | ssh remotehost 'base64 -d | sudo bash'</code></p> <p>Is there a solution? Or do you have a better idea?</p> <br /><h3>回答1:</h3><br /><p>You can do something like this:</p> <p>inline.sh:</p> <pre><code>#!/usr/bin/env bash while read line; do if [[ "$line" =~ (\.|source)\s+.+ ]]; then file="$(echo $line | cut -d' ' -f2)" echo "$(cat $file)" else echo "$line" fi done &lt; "$1" </code></pre> <p>Note this assumes the <code>source</code>d files exist, and doesn't handle errors. You should also handle possible hashbangs. If the <code>sourced</code> files contain themselves <code>source</code>, you need to apply the script recursively, e.g. something like (not tested):</p> <pre><code>while egrep -q '^(source|\.)' main.sh; do bash inline.sh main.sh &gt; main.sh done </code></pre> <p>Let's test it</p> <p>main.sh:</p> <pre><code>source a.sh . b.sh echo cc echo "$var_a $var_b" </code></pre> <p>a.sh:</p> <pre><code>echo aa var_a="stack" </code></pre> <p>b.sh:</p> <pre><code>echo bb var_b="overflow" </code></pre> <p>Result:</p> <pre><code>bash inline.sh main.sh echo aa var_a="stack" echo bb var_b="overflow" echo cc echo "$var_a $var_b" bash inline.sh main.sh | bash aa bb cc stack overflow </code></pre> <p>BTW, if you just want to see what bash executes, you can run</p> <pre><code>bash -x [script] </code></pre> <p>or remotely</p> <pre><code>ssh user@host -t "bash -x [script]" </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/37531927/replacing-source-file-with-its-content-and-expanding-variables-in-bash</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/bash" hreflang="zh-hans">bash</a></div> <div class="field--item"><a href="/tag/set" hreflang="zh-hans">set</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> <div class="field--item"><a href="/tag/subshell" hreflang="zh-hans">subshell</a></div> </div> </div> Mon, 07 Dec 2020 23:55:09 +0000 点点圈 3956580 at https://www.e-learn.cn Approximation of e^x using Maclaurin Series in Python https://www.e-learn.cn/topic/3659117 <span>Approximation of e^x using Maclaurin Series in Python</span> <span><span lang="" about="/user/28" typeof="schema:Person" property="schema:name" datatype="">拈花ヽ惹草</span></span> <span>2020-06-01 12:40:56</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm trying to approximate e^x using the Maclaurin series in a function called <code>my_exp(x)</code>, I believe everything I've done so far is right but I'm getting incorrect approximations for whatever number I try.</p> <pre><code>import math for i in range (x): exp = 1 + ((x**i)/math.factorial(i)) print(exp) </code></pre> <p>For example, whenever I try <code>my_exp(12)</code> I get 18614.926233766233 instead of 162754.79141900392 Help?</p> <br /><h3>回答1:</h3><br /><p>Your problem is that the e^x series is an infinite series, and so it makes no sense to only sum the first x terms of the series.</p> <pre><code>def myexp(x): e=0 for i in range(0,100): #Sum the first 100 terms of the series e=e+(x**i)/math.factorial(i) return e </code></pre> <p>You can also define the precision of your result and get a better solution.</p> <pre><code>def myexp(x): e=0 pres=0.0001 s=1 i=1 while s&gt;pres: e=e+s s=(x**i)/math.factorial(i) i=i+1 return e </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>In order to accumulate the terms of the series, you need to replace the assignment to <code>exp</code> with a line such as:</p> <pre><code>exp = exp + ((x**i)/math.factorial(i)) </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/24915194/approximation-of-ex-using-maclaurin-series-in-python</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/series" hreflang="zh-hans">series</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> <div class="field--item"><a href="/tag/exponential" hreflang="zh-hans">exponential</a></div> </div> </div> Mon, 01 Jun 2020 04:40:56 +0000 拈花ヽ惹草 3659117 at https://www.e-learn.cn Approximation of e^x using Maclaurin Series in Python https://www.e-learn.cn/topic/3659115 <span>Approximation of e^x using Maclaurin Series in Python</span> <span><span lang="" about="/user/177" typeof="schema:Person" property="schema:name" datatype="">天涯浪子</span></span> <span>2020-06-01 12:40:11</span> <div class="field field--name-body field--type-text-with-summary field--label-hidden field--item"><h3>问题</h3><br /><p>I'm trying to approximate e^x using the Maclaurin series in a function called <code>my_exp(x)</code>, I believe everything I've done so far is right but I'm getting incorrect approximations for whatever number I try.</p> <pre><code>import math for i in range (x): exp = 1 + ((x**i)/math.factorial(i)) print(exp) </code></pre> <p>For example, whenever I try <code>my_exp(12)</code> I get 18614.926233766233 instead of 162754.79141900392 Help?</p> <br /><h3>回答1:</h3><br /><p>Your problem is that the e^x series is an infinite series, and so it makes no sense to only sum the first x terms of the series.</p> <pre><code>def myexp(x): e=0 for i in range(0,100): #Sum the first 100 terms of the series e=e+(x**i)/math.factorial(i) return e </code></pre> <p>You can also define the precision of your result and get a better solution.</p> <pre><code>def myexp(x): e=0 pres=0.0001 s=1 i=1 while s&gt;pres: e=e+s s=(x**i)/math.factorial(i) i=i+1 return e </code></pre> <br /><br /><br /><h3>回答2:</h3><br /><p>In order to accumulate the terms of the series, you need to replace the assignment to <code>exp</code> with a line such as:</p> <pre><code>exp = exp + ((x**i)/math.factorial(i)) </code></pre> <br /><br /><p>来源:<code>https://stackoverflow.com/questions/24915194/approximation-of-ex-using-maclaurin-series-in-python</code></p></div> <div class="field field--name-field-tags field--type-entity-reference field--label-above"> <div class="field--label">标签</div> <div class="field--items"> <div class="field--item"><a href="/tag/python" hreflang="zh-hans">python</a></div> <div class="field--item"><a href="/tag/series" hreflang="zh-hans">series</a></div> <div class="field--item"><a href="/tag/expansion" hreflang="zh-hans">expansion</a></div> <div class="field--item"><a href="/tag/exponential" hreflang="zh-hans">exponential</a></div> </div> </div> Mon, 01 Jun 2020 04:40:11 +0000 天涯浪子 3659115 at https://www.e-learn.cn