#!/bin/bash  while read p do   echo "$p" done &lt; numbers.txt   This script is only reading and allowing me to print .sh files. I have tried it with .txt files (like above but it does not print anything.)
122931
I have a file named /tmp/urlFile where each line represents a url. I am trying to read from the file as follows:  cat "/tmp/urlFile" | while read url do     echo $url done   If the last line doesn't end with a newline character, that line won't be read. I was wondering why?   Is it possible to read all the lines, regardless if they are ended with a new line or not?
117882
I tried to use at from within a script of mine and it prints:       warning: commands will be executed using /bin/sh   How would I use different shell if I wanted?
2107
I'm looking for the basic loop like:  for(int i = 0; i &lt; MAX; i++) {   doSomething(i); }   but for bash.
83398
I'm using bash script on AMazon Linux.  When I want to redirect stderr and stdout to a file, I can run  ./my_process.pl &amp;&gt;/tmp/out.txt   Is there a way I can redirect this output to a file and continue to see it on the console (in my shell)?  How is this done?
55899
I have written a small script to count number of occurrences of logs in a particular file. The following is my code:   #!/bin/sh count=1 while read line do     found=$( grep M $line )     OCCURENCE=$( grep M /log.txt)     if [ "$OCCURENCE" ]; then         echo $count")" $line &gt;&gt; filesave.txt     fi     count=$((count+1)) done &lt; log1.txt   but I am getting this error:  ./count.sh ./count.sh: syntax error at line 10: `found=$' unexpected
83042
Is there a way to copy the currently written content of the bash prompt? Say I wrote:  ping www.google.com   so that the lines in terminal looks like:  petr@sova:~$ ping www.google.com   and didn't hit return yet. I want to copy that text to clipboard. Is that possible?  The usual usecase would be hitting up several times and finding a command which I want to copy.
69637
i am writing a script in bash and I would like to give the two numbers in the brackets, x and y to be precise, as variables.  I wrote the script this way:  echo "x?" read x echo "y" read y  for i in {$x..$y}   but it does not work.  How could I set it up to make it function?  Thanks a lot in advance.
28084
Why is this shell script printing inputs twice?  I expected the script to ignore the inputs after 5.  Script:  #! /bin/bash echo "Enter 5 words : " read  a b c d e  printf "&gt; %s %s %s %s %s &lt;" $a $b $c $d $e   Output:  user@linux:~$ pico ifs2.sh user@linux:~$ ./ifs2.sh Enter 5 words :  1 2 3 4 5  &gt; 1 2 3 4 5 &lt;user@linux:~$ ./ifs2.sh Enter 5 words :  1 2 3 4 5 6 &gt; 1 2 3 4 5 &lt;&gt; 6     &lt;user@linux:~$ ./ifs2.sh Enter 5 words :  1 2 3 4 5 6 7 8 9 0 &gt; 1 2 3 4 5 &lt;&gt; 6 7 8 9 0 &lt;user@linux:~$    And, the following script works no matter what is set to $IFS. Why?  #! /bin/bash     old="$IFS" IFS=":" echo "IFS = $IFS" echo "Enter 5 words : " read  a b c d e  printf "&gt; %s %s %s %s %s &lt;" $a $b $c $d $e     IFS="$old"   Output:  user@linux:~$ ./ifs2.sh IFS = : Enter 5 words :  1 2 3 4 5   &gt; 1 2 3 4 5      &lt;user@linux:~$ ./ifs2.sh IFS = : Enter 5 words :  1 2 3 4 5 &gt; 1 2 3 4 5     &lt;user@linux:~$ ./ifs2.sh IFS = : Enter 5 words :  1:2:3:4:5 &gt; 1 2 3 4 5 &lt;user@linux:~$
115643
If I just use basename {} .txt, it will work:  find . -iname "*.txt" -exec basename {} .txt \;   It will just print xxx instead of ./xxx.txt  if I want use $(basename {} .txt) in the -exec option, it will fail:  find . -iname "*.txt" -exec echo "$(basename {} .txt)" \;   It will just print ./xxx.txt  How can I solve this problem? I hope I can use $(basename {} .txt) as parameters for other cmd. Do I have to do sh -c or pipe -exec basename {} \; with xargs?
37727
Why does running echo $$ in bash return a number like 7190, while running echo $ only returns a $?
77730
Let's suppose I've declared the following variables:  $ var='$test' $ test="my string"   If I print their contents I see the following:  $ echo $var $test  $ echo $test my string   I'd like to find a way to print the content of the content of $var (which is the content of $test). So I tried to do the following:  $ echo $(echo $var) $test   But here the result is $test and not "my string"... Is it possible to print the content of the content of variables using bash?
723
I am trying to understand how exactly Bash treats the following line:  $(&lt; "$FILE")   According to the Bash man page, this is equivalent to:  $(cat "$FILE")   and I can follow the line of reasoning for this second line. Bash performs variable expansion on $FILE, enters command substitution, passes the value of $FILE to cat, cat outputs the contents of $FILE to standard output, command substitution finishes by replacing the entire line with the standard output resulting from the command inside, and Bash attempts to execute it like a simple command.  However, for the first line I mentioned above, I understand it as: Bash performs variable substitution on $FILE, Bash opens $FILE for reading on standard input, somehow standard input is copied to standard output, command substitution finishes, and Bash attempts to execute the resulting standard output.  Can someone please explain to me how the contents of $FILE goes from stdin to stdout?
47782
I am trying out the command  $ b=5; echo `$b`; -bash: 5: command not found   but it does not print 5 as it is supposed to. What am I missing here?   seems to say that ` evaluates the commands within and replaces them with the output.
28963
I thought this would be simple - but it is proving more complex than I expected.  I want to iterate through all the files of a particular type in a directory, so I write this:  #!/bin/bash  for fname in *.zip ; do    echo current file is ${fname} done   This works as long as there is at least one matching file in the directory. However if there are no matching files, I get this:  current file is *.zip   I then tried:  #!/bin/bash  FILES=`ls *.zip` for fname in "${FILES}" ; do     echo current file is ${fname} done   While the body of the loop does not execute when there are no files, I get an error from ls:  ls: *.zip: No such file or directory   How do I write a loop which cleanly handles no matching files?
44251
In Bash, piping tail -f to a read loop blocks indefinitely.   while read LINE0  do      echo "${LINE0}";  done &lt; &lt;( tail -n 3 -f /tmp/file0.txt | grep '.*' )  # hangs   Remove the -f or | grep '.*', then the loop will iterate.   The following does not hang.  tail -n 3 -f /tmp/file0.txt | grep '.*'    What causes this behavior?    Is there anyway in Bash to follow a file and read in a pipe expression?
12724
Why do I get different values for $x from the snippets below?  #!/bin/bash  x=1 echo fred &gt; junk ; while read var ; do x=55 ; done &lt; junk echo x=$x  #    x=55 .. I'd expect this result  x=1 cat junk | while read var ; do x=55 ; done echo x=$x  #    x=1 .. but why?  x=1 echo fred | while read var ; do x=55 ; done echo x=$x  #    x=1  .. but why?
26310
I have a file servers.txt, with list of servers:  server1.mydomain.com server2.mydomain.com server3.mydomain.com   when I read the file line by line with while and echo each line, all works as expected. All lines are printed.  $ while read HOST ; do echo $HOST ; done &lt; servers.txt server1.mydomain.com server2.mydomain.com server3.mydomain.com   However, when I want to ssh to all servers and execute a command, suddenly my while loop stops working:  $ while read HOST ; do ssh $HOST "uname -a" ; done &lt; servers.txt Linux server1 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux   This only connects to the first server in the list, not to all of them. I don't understand what is happening here. Can somebody please explain?  This is even stranger, since using for loop works fine:  $ for HOST in $(cat servers.txt ) ; do ssh $HOST "uname -a" ; done Linux server1 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux Linux server2 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux Linux server3 2.6.30.4-1 #1 SMP Wed Aug 12 19:55:12 EDT 2009 i686 GNU/Linux   It must be something specific to ssh, because other commands work fine, such as ping:  $ while read HOST ; do ping -c 1 $HOST ; done &lt; servers.txt