Per  I try to write callbacks (mostly for learning purposes before I proceed to promises) The Right Way(tm).  OK so I have this:  "use strict";  var fs = require('fs');  var readFileDone = function(err, data) {     if(err) return console.error(err);     console.log('read done');     writeFile(null, data); };   var writeFile = function(err, data) {     if(err) return console.error(err);     var ws = fs.createWriteStream('/tmp/copy.txt')     ws.end(data, 'utf-8', writeFileDone); };  var writeFileDone = function(err, data) {     console.log('writing file done'); };  fs.readFile('test_underscore1.js', readFileDone);   I run this using ST3's build system I defined for Javascript like so:  {   "cmd": ["/usr/bin/nodejs", "$file"],   "selector": "source.js" }   Now the problem: if I move line fs.readFile('test_underscore1.js', readFileDone); above callback definitions, it does not run (no output in console and the file in /tmp does not get created either).  Why is that? After all, Javascript is supposed to parse entire file including function definitions before running? Therefore, the position of the line calling fs.readFile should not matter?
647
I've recently started maintaining someone else's JavaScript code. I'm fixing bugs, adding features and also trying to tidy up the code and make it more consistent.  The previous developer used two ways of declaring functions and I can't work out if there is a reason behind it or not.  The two ways are:  var functionOne = function() {     // Some code };     function functionTwo() {     // Some code }   What are the reasons for using these two different methods and what are the pros and cons of each? Is there anything that can be done with one method that can't be done with the other?
97954
I want to create a compiled JavaScript file for my website.  For development I would prefer to keep the JavaScript in separate files and just as part of my automated scripts  concatenate the files into one and run the compressor over it.  My problem is that if I use the old DOS copy command it also puts in the EOF markers which the compressor complains about:  copy /A *.js compiled.js /Y  What are other people doing?
117426
Why when I try to write a function just in one line into .bashrc file,  list(){ ls -a }   I get error?  bash: /home/username/.bashrc: line num: syntax error: unexpected end of file   but when I write it in multi line it's ok?  list(){     ls -a }
24776
I need to spawn a child process that is a console application, and capture its output.  I wrote up the following code for a method:  string retMessage = String.Empty; ProcessStartInfo startInfo = new ProcessStartInfo(); Process p = new Process();  startInfo.CreateNoWindow = true; startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardInput = true;  startInfo.UseShellExecute = false; startInfo.Arguments = command; startInfo.FileName = exec;  p.StartInfo = startInfo; p.Start();  p.OutputDataReceived += new DataReceivedEventHandler (     delegate(object sender, DataReceivedEventArgs e)     {         using (StreamReader output = p.StandardOutput)         {             retMessage = output.ReadToEnd();         }     } );  p.WaitForExit();  return retMessage;   However, this does not return anything. I don't believe the OutputDataReceived event is being called back, or the WaitForExit() command may be blocking the thread so it will never callback.  Any advice?  EDIT: Looks like I was trying too hard with the callback. Doing:  return p.StandardOutput.ReadToEnd();    Appears to work fine.
3033
I've always handled optional parameters in JavaScript like this:  function myFunc(requiredArg, optionalArg){   optionalArg = optionalArg || 'defaultValue';    // Do stuff }   Is there a better way to do it?  Are there any cases where using || like that is going to fail?
62738
Let's say I compile the following file with pdflatex -shell-escape test.tex:   \documentclass{minimal}  \begin{document}  File listing is:  \immediate\write18{ls /usr}  \end{document}   This will send the output of the command ls /usr to report/log of pdflatex (primarily to stdout).   There are then two cases I'd like to utilize:    The output of ls /usr being included directly in the document (LaTeX stream). The output of ls /usr becoming the contents of a \newcommand (by this I mean, that I'd like the script to be executed when the \newcommand is executed first - and upon subsequent calls to the new command, the shell code should not be executed anew).    I have read through , but I'm not sure if this "pipe input" can be applied to \newcommand.   I have also read through  - and it seems that Tex' file I/O should be used; but I'm quite disliking the fact that I'd still have to redirect the script output (actually, in this case, the ls /usr output) to a file, and then read it in, to have it as contents of a command.   So, is there an easier way to achieve what I'd want (hopefully, illustrated through an example based on the above code)?     EDIT: Ehm, I should have asked one more question earlier :) I'll try with an edit here, although it will probably get missed .. :)   I originally asked for a \newcommand that will execute shell code only upon its definition (i.e., in a sense it is "cached"); and the answer from @egreg does exactly that. But then - would it be possible to have a different \newcommand definition, such that each time this newcommand is called, the shell command is executed anew? I.e. executing something like \@@input|"cat tempfile" (which cannot be executed as such), where tempfile changes between calls?
46307
I wanted to capture to a file the errors being returned on the command line from grep. For example,  grep foo.lookup No such file in directory   I want to output that to a log file. This is my shell script:  lookUpVal=1 var1=$(grep $lookUpVal foo.lookup) &gt;&gt;lookup.log 2&gt;$1   It creates the file lookup.log but doesn't write the error on it.
78588
How do you tell if a function in JavaScript is defined?  I want to do something like this  function something_cool(text, callback) {     alert(text);     if( callback != null ) callback(); }   But it gets me a     callback is not a function   error when callback is not defined.
51606
Possible Duplicate:          This is something I haven't quite figured out yet, but I have been using function(){}() just because my VIM syntax highlight screws up if I add the parenthesis, although I've seen (function(){})() around many times, maybe its an IE thing?  edit:  var singleton = function() {     // code }();  var singleton = (function() {     // code })();
84459
I was writing a bash script, and happened to updated the code (saved the script file to disk) while the script was waiting for some input in a while loop. After I returned to the terminal and continued with the previous invocation of the script, bash gave an error about the file syntax:  /home/aularon/bin/script: line 58: unexpected EOF while looking for matching `"' /home/aularon/bin/script: line 67: syntax error: unexpected end of file   So I tried doing the following:  1st: create a script, self-update.sh let's call it:  #!/bin/bash fname=$(mktemp) cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname cp $fname $0 rm -f $fname echo 'String: BEFORE.';   What the script does is reading its code, changing the word 'BEFORE' into 'AFTER', then re-write itself with the new code.  2nd Run it:  chmod +x self-update.sh ./self-update.sh   3rd wonder...  aularon@aularon-laptop:~$ ./self-update.sh  String: AFTER!.   Now, I wouldn't have guessed that on the same invocation it would output AFTER!, on the second run for sure, but not on the first one.  So my question is: is it intentional (by design)? or it is because of the way bash runs the script? Line by line or command by command. Is there any good use of such behaviour? Any example of it?    Edit: I tried reformatting the file to put all command in one line, it does not work now:  #!/bin/bash fname=$(mktemp);cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname;cp $fname $0;rm -f $fname;echo 'String: BEFORE.';   Output:   aularon@aularon-laptop:~$ ./self-update.sh #First invocation String: BEFORE. aularon@aularon-laptop:~$ ./self-update.sh #Second invocation String: AFTER!.   While when moving the echo string to the next line, separating it from the rewriting (cp) call:  #!/bin/bash fname=$(mktemp);cat $0 | sed 's/BEFORE\./AFTER!./' &gt; $fname;cp $fname $0;rm -f $fname; echo 'String: BEFORE.';   And now it works again:  aularon@aularon-laptop:~$ ./self-update.sh  String: AFTER!.
122801
Using receipt of "" I elaborated  the following code for my needs:  \documentclass{book}  ... \usepackage{expl3}  \begin{document}  \newcommand{\PrintAnswer}[1]{% \InputIfFileExists{#1}{\refstepcounter{subsection}}{\typeout{*** #1 not found ***}}}  \ExplSyntaxOn \cs_new:Nn\PrintAnswerList:n{   \clist_set:Nx\l_csv_clist{#1}   \clist_map_inline:Nn\l_csv_clist{     \typeout{**** Printing ##1.ans}     \ExplSyntaxOff     \PrintAnswer{##1.ans}     \ExplSyntaxOn   } } \PrintAnswerList:n{\inputfiles} \ExplSyntaxOff  \end{document}    It is intended for conditional compilation of a textbook, every chapter of which (eg, 01.tex, 02.tex) writes answers to problems to the file named after the name of the chapter source file, (eg, 01.ans, 02.ans etc). Near the end of the book these files are read in by the macro \PrintAnswer. Usually, I compile only few chapters using the following trick to keep desired chapters in \inputfiles macro:  \typein[\inputfiles]{^^JEnter filename(s) for \protect\includeonly:}   All that works fine, but I was forced to switch off experimental syntax before \PrintAnswer{##1.ans} because otherwise answer files are not processed correctly (in particular, LaTeX complains that the commands for greek letters are not defined and hyphenation is broken). Therefore my question is How can one rewrite the above code using user-level LaTeX3 commands?  I found \SplitList command in xparse package. Can it help?
1224
Im having this weird error that says:  Underfined Control Sequence. 1.60 \@writefi   I can press [s] on the command line and the message is ignored, and the code still runs ...  What can be causing this error?
38774
I was looking through some code from the  library and came across a function declared like this:  LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)   Now, I'm a Delphi coder.  No hablo C muy bien, senor.  But I remember enough syntax from my college courses to read it like this:  Function name is WndProc.  Argument list is pretty self-explanatory.  Function return type is LRESULT.  But what in the world is that "CALLBACK" doing there?  In Delphi, any function can be used as a callback; you just need to pass the right type of function pointer.  Is there any particular reason why C doesn't work that way?  Or does it mean something different?
20165
I have written a simple script.  When I runsh &lt;myscriptname.sh&gt;, i got the correct output, but when I run ./&lt;myscriptname.sh&gt;, I got an error.  What is difference between when I do sh and ./?
104469
Is there a way to pass a call back function in a Java method?  The behavior I'm trying to mimic is a .Net Delegate being passed to a function.  I've seen people suggesting creating a separate object but that seems overkill, however I am aware that sometimes overkill is the only way to do things.
69182
I'd like to print a man style usage message to describe a shell function like this output man find:  NAME        find - search for files in a directory hierarchy  SYNOPSIS        find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]  DESCRIPTION        This  manual  page  documents the GNU version of find.  GNU find searches the directory tree rooted at each        given starting-point by evaluating the given expression from left to  right,  according  to  the  rules  of        precedence  (see section OPERATORS), until the outcome is known (the left hand side is false for and opera‐        tions, true for or), at which point find moves on to the next file name.  If no  starting-point  is  speci‐        fied, `.' is assumed.  OPTIONS   I am facing an error message on the ` character. Following simple script shows the error:  ~$ cat &lt;&lt;EOF `.' EOF  bash: bad substitution: no closing "`" in `.'   I though heredoc was a cool way to echo strings by pasting them without having to escape its content such a quotes, etc... I assume I was wrong :/  Can someone explain this behavior please? Can heredoc accept ` character?  Edit 2: I accepted the answer of quoted here-document  &lt;&lt;'END_HELP', but I finally won't use it for this kind of complete manual output as  does   Edit 1: (For future reads) the limit with using quoted here-document is that is prevents to use tput in the here-document. To do so, I did the following:   unquoted here-document, for tput commands to be executed prevent the "bad substitution" error by escaping the backtick instead use tput within the  here-document   Example:  normal=$( tput sgr0 ) ; bold=$(tput bold) ;  cat &lt;&lt;END_HELP # here-document not quoted ${bold}NAME${normal}        find - search for files in a directory hierarchy  ${bold}SYNOPSIS${normal}        find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [expression]  ${bold}DESCRIPTION${normal}        This  manual  page  documents the GNU version of find.  GNU find searches the directory tree rooted at each        given starting-point by evaluating the given expression from left to  right,  according  to  the  rules  of        precedence  (see section OPERATORS), until the outcome is known (the left hand side is false for and opera‐        tions, true for or), at which point find moves on to the next file name.  If no  starting-point  is  speci‐        fied, \`.' is assumed. END_HELP  unset normal ; unset bold ;   Here, note the escaped backtick that was source of error:  \`.'
49191
I was trying to redirect both stdout and stderr to a file today, and I came across this:  &lt;command&gt; &gt; file.txt 2&gt;&amp;1   This apparently redirects stderr to stdout first, and then the resulting stdout is redirected to file.txt.  However, why isn't the order &lt;command&gt; 2&gt;&amp;1 &gt; file.txt? One would naturally read this as (assuming execution from left to right) the command being executed first, the stderr being redirected to stdout and then, the resulting stdout being written to file.txt. But the above only redirects stderr to the screen.  How does the shell interpret both the commands?