I'm not a master in Scripting, so I have to ask here :)  How can I implement an errorhandling for this:  find . -type f|sed 's_\.\/__' &gt; $PATH   i.e. abort when this was not successfull.  Thank you very much in advance Thomas
1400
What is your favorite method to handle errors in Bash?  The best example of handling errors I have found on the web was written by William Shotts, Jr at .   He suggests using the following function for error handling in Bash:  #!/bin/bash  # A slicker error handling routine  # I put a variable in my scripts named PROGNAME which # holds the name of the program being run.  You can get this # value from the first item on the command line ($0).  # Reference: This was copied from &lt;http://www.linuxcommand.org/wss0150.php&gt;  PROGNAME=$(basename $0)  function error_exit {  #   ---------------------------------------------------------------- #   Function for exit due to fatal program error #       Accepts 1 argument: #           string containing descriptive error message #   ----------------------------------------------------------------       echo "${PROGNAME}: ${1:-"Unknown Error"}" 1&gt;&amp;2     exit 1 }  # Example call of the error_exit function.  Note the inclusion # of the LINENO environment variable.  It contains the current # line number.  echo "Example of error with line number and message" error_exit "$LINENO: An error has occurred."   Do you have a better error handling routine that you use in Bash scripts?
40363
I'm trying to replace each , in the current file by a new line:  :%s/,/\n/g    But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.  What should I do?  If you are curious, like me, check the question  as well.
26898
I have this script to divide a file over a 100000 or even more in 50000 lines.  desc()                                                     {                                                       echo $1 $2|awk '{d=$1;}                                 BEGIN {a=1;b=0}                                            { f=d"0"a;                                                while ((getline x&lt;d)&gt;0)                                   { print x&gt;&gt;f".txt"                                        if (b++&gt;=50000)                                            {                                                         close(f);b=0;a++;f=(a&lt;10)?d"0"a:f=d""a                }                                                  }                                                   }                                                   END {print "Terminado ... ! "}'                         }                                                      if [ -f $1 ]; then                                              desc $1 $2                                      fi                                                        When I execute it I got this error message:  sh-3.2$ parte.sh pa.txt                                  : command not found                                      '/parte.sh: line 2: syntax error near unexpected token ` '/parte.sh: line 2: `desc()                              sh-3.2$                                                    Can anyone help to solve it?
122379
I am trying to write a script that will delete all comments and everything in between inside C files in my current directory. I've been using sed, and this is what I have so far:   sed -i '/ * [^()] */d' *.c  This works when the comments are on the same line as an asterisk or backslash.   However it doesn't work when there is a commented line without a slash or asterisk.   I know sed goes line by line, I just don't know how to tell it to keep deleting until it sees a */.
85712
When using the Text Editor, what is the best command to include inside a script which will abort the script at that line?  quit() and sys.exit() stop the script, but also kill Blender.
54821
I know this question has probably been answered before.  I have seen many threads about this in various places, but the answers are usually hard to extract for me.  I am looking for help with an example usage of the 'sed' command.  Say I wanted to act upon the file "hello.txt" (in same directory as prompt).  Anywhere it contained the phrase "few", it should be changed to "asd".  What would the command look like?
34120
I am looking for a way to replace a string in a file with a string that contains a slash by using sed.  connect="192.168.100.61/foo" srcText="foo.bar=XPLACEHOLDERX" echo $srcText | sed "s/XPLACEHOLDERX/$connect"   The result is:  sed: -e Expression #1, Character 32: Unknown option for `s'
84698
I'm looking to write a script that will find and replace any characters other than [^A-Za-z0-9._-] in a specific file type (in this case .avi) with an underscore _. I want this to exec recursively from the current path. I'm using Ubuntu.  I do not want or need to change any folder names as I have control over those at the time of creation. Here's what I have now after referencing the following link:    find . -depth -exec rename 's!([^/]*\Z)/[^A-Za-z0-9._-]/_/g' *.avi {} +   I'm clearly missing something. Please help. :)
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:  \`.'
99810
I run this command:  ~/shell_temp$ find . -type f -name "IMAG1806.jpg" -exec rm -f {}\   i got output below:  &gt; IMAG1806.jpg  Error: find: missing argument to `-exec'   what is exact command for find any file from current directory and remove with -exec?
55053
I would like to know what it really does with following scripts used with sed command.  sed -e 's:&lt;F0_M&gt;:&lt;o,f0,male&gt;:' \           -e 's:&lt;F0_F&gt;:&lt;o,f0,female&gt;:' \           -e 's:([0-9])::g' \           -e 's:&lt;sil&gt;::g' \           -e 's:([^ ]*)$::' | \   First and second scripts looks like we are transforming text of type &lt;F0_F&gt; to &lt;o,f0,female&gt;. But what about the last three where it involves '::','g' and '$' sign. In most of the documentations they have used '\' and '/' in most of scripts. But here they have used ':' instead of slashes. Can some one explain above three scripts?
104573
Even when /tmp has no file called something, searching for it with find will return 0:    $ find /tmp -name something    $ echo $?   0   How can I get a non-zero exit status when find does not find anything?
104529
I have seen this construct in scripts a lot and used it myself, but it bothers me that I can't seem to find it in the documentation.  Example:  [ -f file1 ] &amp;&amp; [ -f file2 ] &amp;&amp; echo "Both files exist." || echo "One or the other file doesn't exist."   This could also be done with backslashes before the newlines, as mentioned in man bash:  If a \&lt;newline&gt; pair appears,  and  the  backslash  is  not itself  quoted,  the \&lt;newline&gt; is treated as a line continuation (that is, it is removed from the input stream and effectively ignored).   Example:  [ -f file1 ] &amp;&amp; \ [ -f file2 ] &amp;&amp; \ echo "Both files exist." || \ echo "One or the other file doesn't exist."   ...but this doesn't seem to be necessary.  The first version above works even without the backslashes.  Where can I find this in man bash?  (Also, is this bash specific or POSIX compliant?)
48946
Due to an application bug as yet undiagnosed, I have several hundred servers with a full disk.  There is one file that has been filled up with duplicate lines—not a log file, but a user environment file with variable definitions (so I can't just delete the file).  I wrote a simple sed command to check for the erroneously added lines and delete them, and tested it on a local copy of the file.  It worked as intended.  However, when I tried it on the server with the full disk, I got approximately the following error (it's from memory, not copy and paste):  sed: couldn't flush /path/to/file/sed8923ABC: No space left on deviceServerHostname   Of course, I know there's no space left.  That's why I'm trying to delete stuff!  (The sed command I'm using will reduce a 4000+ line file to about 90 lines.)  My sed command is just sed -i '/myregex/d' /path/to/file/filename  Is there a way I can apply this command despite the full disk?  (It must be automated, since I need to apply it to several hundred servers as a quick-fix.)  (Obviously the application bug needs to be diagnosed, but in the meantime the servers aren't working correctly....)    Update: The situation I faced was resolved by deleting something else that I found out I could delete, but I'd still like the answer to this question, which would be helpful in the future and for other people.  /tmp is a no-go; it's on the same filesystem.  Before I freed up disk space, I did test and find out that I could delete the lines in vi by opening the file and running :g/myregex/d and then successfully save the changes with :wq.  It seems it should be possible to automate this, without resorting to a separate filesystem to hold a temp file....  (?)
104690
I want to sed using a variable.  For example, sed -n '1,10p' file. So, instead of 10, I want to use $k where k is an integer.
32406
Technically, any odd number of backslashes, as described in .  &gt;&gt;&gt; r'\'   File "&lt;stdin&gt;", line 1     r'\'        ^ SyntaxError: EOL while scanning string literal &gt;&gt;&gt; r'\\' '\\\\' &gt;&gt;&gt; r'\\\'   File "&lt;stdin&gt;", line 1     r'\\\'          ^ SyntaxError: EOL while scanning string literal   It seems like the parser could just treat backslashes in raw strings as regular characters (isn't that what raw strings are all about?), but I'm probably missing something obvious.
51340
I want to watch a file until a bit of text appears  I found this answer:   but when I tried it on ubuntu, it doesn't exit:  $ echo one &gt; test.txt $ echo two &gt;&gt; test.txt $ echo three &gt;&gt; test.txt $ echo four &gt;&gt; test.txt $ cat test.txt | sed '/w/ q' one two $   works as expected.  however when I try to tail the file  $ tail -f test.txt | sed '/w/ q' one two   it never exits.  tail does not stop even though the pipe is broken.  Does anybody know how to make tail exit when sed exits?