I have a script named temp.sh. I am running it from another script like...  sh temp.sh | tr '|' '\n' &gt; sel-employee count=`wc -l sel-employee`  if [ '$count' == 0 ] ; then  echo "ERROR" else echo "SUCCESS" fi   when I run this script if file  sel-employee has zero line OR more then zero line in both cases it is output  SUCCESS. I can not understand why?
6324
Sometimes I see shell scripts use all of these different ways of quoting some text: &quot;...&quot;, '...', $'...', and $&quot;...&quot;. Why are there so many different kinds of quote being used? Do they behave differently or affect what I can do inside them?
46113
I am new to shell scripts and many books have written that use #!(sha-bang) line in starting of the script to invoke the interpreter .And this will invoke a new shell for script and do the interpretation line by line.But one of my basic script is still running without the magic line.  so my questions are:   from where did my basic script get the interpreter. how did the script manages to locate the interpreter.   now let me tell you about my basic script it just contains the following line:  echo "basic script without the magic lines"
48290
What's the difference between temp=$1 and temp=1? Why do I need the dollar sign?  For example:   #!/bin/bash  temp=$1 cell=$((($temp-32)*5/9))  echo $cell
100567
This is from . It's about executing a Perl script.     Finally, if you are unfortunate enough to be on an ancient Unix system   that doesn’t support the magic #! line, or if the path to your   interpreter is longer than 32 characters (a built-in limit on many   systems), you may be able to work around it like this:   #!/bin/sh -- # perl, to stop looping eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0;   Can you explain step by step what's going on here? I'm trying to make it work or encompass, but to no avail.  At execution of just the above, I'm getting this:  /bin/sh: 0: Illegal option --
78668
I encountered a behaviour that I don't understand while testing a script that sums the outputs from repeated executions of a program. To reproduce it create the text files out, which represents the output of my program, and sum, the file that holds the sum of the values returned on previous executions and which starts out as a copy of out,  cat &gt; out &lt;&lt; EOF 2 20 5 50 EOF cp out sum   The strange thing happens on running  paste out sum | awk '{$1 += $3; $2 += $4; NF = 2; print}' | tee sum   several times (15-20 times might be needed). Each time it runs, this command should add to the values in sum the corresponding values in out and write the results back to sum. What I get is that it works an unpredictable number of times, then sum reverts back to  2 20 5 50   I have later learned that  and solved the issue using a temporary file, still, this behaviour baffles me:   why does … | tee sum work at all (even if only for a limited number of iterations), while … &gt; sum never overwrites sum? why doesn't it work a predictable number of times?
45767
I am seeing different results from the find command between being run manually on the command line vs run in a script.   If I run the following on the command line:   find . -name '*.txt' ! -name '*20120427.txt'    I get all the .txt files in a directory except for the ones with the date that I want to avoid.  But when I have this same line in a script with a variable to identify the date:  FDATE=`date +%Y%m%d` find . -name '*.txt' ! -name '*${FDATE}.txt'    The script finds all the .txt files including the ones with the date I want to avoid.   The only difference between the 2 is that I am passing the FDATE variable in the script. Is there something obvious I am missing? I do not understand why the results are coming out different.
121409
My script (should) acts differently, depending on the presence of the data in the input stream. So I can invoke it like this:  $ my-script.sh   or:  $ my-script.sh &lt;&lt;-MARK     Data comes...     ...data goes. MARK   or:  $ some-command | my-script.sh   where two last cases should read the data, while first case should notice the data is missing and act accordingly.  The crucial part (excerpt) of the script is:  #!/bin/bash local myData; read -d '' -t 0 myData;  [ -z "${myData}" ] &amp;&amp; {     # Notice the lack of the data. } || {     # Process the data. }   I use read to read input data, then option -d '' to read multiple lines, as this is expected, and the -t 0 to set timeout to zero. Why the timeout? According to  (typing left unchanged; bold is mine):     -t timeout time out and return failure if a complete line of input is               not read withint TIMEOUT seconds.  The value of the TMOUT               variable is the default timeout.  TIMEOUT may be a               fractional number.  If TIMEOUT is 0, read returns success only               if input is available on the specified file descriptor.  The               exit status is greater than 128 if the timeout is exceeded   So I in case 2 and 3 it should read the data immediately, as I understand it. Unfortunately it doesn't. As -t can take fractional values (according to above man page), changing read line to:  read -d '' -t 0.01 myData;   actually reads the data when data is present and skips it (after 10ms timeout) if it is not. But it should also work when TIMEOUT is set to real 0.  Why it actually doesn't? How can this be fixed? And is there, perhaps, alternative solution to the problem of "act differently depending on the presence of the data"?  UPDATE  Thanks to @Isaac I found a misleading discrepancy between quoted on-line version and my local one (normally I do not have locale set to en_US, so help read gave me translation which I couldn't paste here, and looking up for on-line translation was faster than setting new env---but that caused the whole problem).  So for 4.4.12 version of Bash it says:     If TIMEOUT is 0, read returns               immediately, without trying to read any data, returning               success only if input is available on the specified               file descriptor.   This gives a little bit different impression than "If TIMEOUT is 0, read returns success only if input is available on the specified file descriptor"---for me it implied actually an attempt to read the data.  So finally I tested this and it worked perfectly:  read -t 0 &amp;&amp; read -d '' myData;   The meaning: see if there's anything to read and if it succeed, just read it.  So as to base question, the correct answer was provided by Isaac. And as to alternative solution I prefer the above "read &amp;&amp; read" method.
778
Usually, $0 in a script is set to the name of the script, or to whatever it was invoked as (including the path). However, if I use bash with the -c option, $0 is set to the first of the arguments passed after the command string:  bash -c 'echo $0' foo bar  # foo    In effect, it seems like positional parameters have been shifted, but including $0. However shift in the command string doesn't affect $0 (as normal):  bash -c 'echo $0; shift; echo $0' foo bar # foo # foo   Why this apparently odd behaviour for command strings? Note that I am looking for the reason, the rationale, behind implementing such odd behaviour.     One could speculate that such a command string wouldn't need the $0 parameter as usually defined, so for economy it is also used for normal arguments. However, in that case the behaviour of shift is odd. Another possibility is that $0 is used to define the behaviour of programs (a la bash called as sh or vim called as vi), but that cannot be, since $0 here is only seen in the command string and not by programs called within it. I cannot think of any other uses for $0, so I am at a loss to explain this.
101982
In Bash I'm executing a command and putting the result in a variable like this:  export var=`svn ls`  But if SVN fails for some reason--say it returns a non-zero error code--export still returns status code 0. How do I detect if the executed command fails?
110061
To output all lines into a file /tmp/ps.txt  $ ps -e &gt;/tmp/ps.txt       To count it with wc -l  $ wc -l /tmp/ps.txt 172   To count it without exporting a file.  $ ps -e | wc -l 173   Why ps -e | wc -l  get one more line?  I don't think  for my question.  $ echo "test" | wc -l 1   Please try it in your terminal, it would yield 2 as .
27803
How can I determine the name of the Bash script file inside the script itself?  Like if my script is in file runme.sh, then how would I make it to display "You are running runme.sh" message without hardcoding that?
91886
I wrote a simple script   #!/bin/bash -x  selentry=`ls -l / | sort ; ts=${PIPESTATUS[0]}`  echo $ts   but $ts show nothing. How I can display $ts variable, or how I can get exit status code from command in variable?  selentry=`ls -l / | sort`
48283
I am taking the Linux Essentials class and was doing well until I hit the scripting chapter. I simply do not understand these concepts.  Wondering if someone can break the following down into ultra simplistic terms or point me to a better reference to learn it. I'm currently using netacad's curriculum.        From the text book (with minor formatting changes):     There are some special variables in addition to the ones you set. You   can pass arguments to your script:  #!/bin/bash echo "Hello $1"       A dollar sign followed by a number N corresponds to the Nth argument   passed to the script. If you call the example above with ./test.sh the   output will be Hello Linux. The $0 variable contains the name of the   script itself.      After a program runs, be it a binary or a script, it returns an exit   code which is an integer between 0 and 255. You can test this through   the $? variable to see if the previous command completed successfully.     I understand how to assign variables and how they work with the $ but the whole issue with $0 and $1 -- I just don't get it.    Any help would be much appreciated.
39563
I have this simple script which redirects the output and append it to a file.  filename="/home/ronnie/tmp/hello"  date=$(date) echo "$date" &gt;&gt; $filename   Now, lets suppose I change date=$(date) to date= $(date) which will generate an error.   My modified script:  filename="/home/ronnie/tmp/hello"  date= $(date) echo "$date" &gt;&gt; $filename 2&gt;&gt; $filename  #Also tried echo "$date" &gt;&gt; $filename 2&gt;&amp;1   I was thinking that above script will redirect the error test.sh: line 5: Fri: command not found to the file hello but it just enters a new line into the file and the error gets printed on my stdout.  My bash version:  ronnier@ronnie:~/tmp$ bash --version GNU bash, version 4.2.24(1)-release (i686-pc-linux-gnu)   So, where am I going wrong.
98935
I have a sample script (credit goes to ):  #!/bin/bash cat &lt;(cat /etc/lsb-release)   I save this script as somename.sh in my home directory.  Now, I try to open this file in three different ways:  sh somename.sh  bash somename.sh  ./somename.sh   So, I have two questions:   Why are the outputs for the above commands different though they are running the same script?   sh produces a Syntax error bash outputs the desired result ./ gives a Permission Denied error  Why is it necessary to give the script executable permission only when running the script using ./ and not necessary in the other cases?   Thanks in advance!  EDIT: The first part might be similar to the  linked one but, I did have a second question too.
82339
I have the following simple script:  #!/bin/bash echo "Bash version ${BASH_VERSION}..." for i in {1..99..2} do         echo $i done   Output in case I run with sh file.sh:  Bash version ... {1..99..2}   Output in case I run with bash file.sh:  Bash version 4.2.25(1)-release... 1 3 5 . . . 99   I have two queries:   If I have written a shabang line specifying shell, shouldn't it run with bash whether I use sh file.sh or bash file.sh? I understand that $BASH_VERSION is not recognized by sh but what is the problem with for loop? Why isn't printing the numbers?
17016
The following  code is supposed to compare two strings or variables. But, I cannot do it. Here, output doesn't depend upon the changing of inputs. Can anyone correct this code?    #!/bin/sh   echo "Enter your username1:"   read username1   echo "Enter your username2:"   read useraname2   if [ "$username1"=="$username2" ]   then   echo 'Same'   else   echo 'Different'   fi