Главная » Ошибки » How to ignore error in command in bash script?

How to ignore error in command in bash script?

How to ignore error in command in bash script?

Tags

shell scripts

Login to Discuss or Reply to this Discussion in Our Community

 

Thread Tools

Search this Thread

Top Forums Shell Programming and Scripting

How to ignore error in command in bash script?

#
1  

12-10-2013

postcd
Banned87,2Member Information Avatar
Join Date: Oct 2013Last Activity: 1 October 2019, 4:32 AM EDT
Posts: 87
Thanks Given: 14

Thanked 2 Times in 2 Posts

How to ignore error in command in bash script?
class=»npb-hr»>

Hello,
i have bash script where im cycling some command for different lines in external file.
example:
Code:while read domain;do
nslookupout=$(nslookup -type=ns $domain) || true
another commands
done < filenamewithdomains

i added:
|| true
after the command in belief it will just skip failures.
But i got:
nslookup: ‘.somedomain.com’ is not a legal name (empty label)
and it breaken running the script..
please how to achieve so this error is skipped and continuing to the next entry?
i know i can fix my domain list so it dont contains inproper values, but i prefer skipping invalid entries. if anyone know how to remove lines in a file containing two dots at one line by sed, please kindly share.

postcd
View Public Profile for postcd

Find all posts by postcd

#
2  

12-10-2013

Akshay HegdeForum Staff
Moderator1,837,668Member Information Avatar
Join Date: Nov 2012Last Activity: 30 June 2020, 12:07 PM EDT
Posts: 1,837
Thanks Given: 180

Thanked 668 Times in 590 Posts

You can do something like this
Code:your command 2>&1 >/dev/null

Akshay Hegde
View Public Profile for Akshay Hegde

Find all posts by Akshay Hegde

#
3  

12-10-2013

hergpRegistered User855,198Member Information Avatar
Join Date: Jan 2010Last Activity: 17 January 2019, 3:37 AM EST
Posts: 855
Thanks Given: 37

Thanked 198 Times in 176 Posts

I don’t see, why your code should not work. The || true even takes care of the situation when the shell’s -e option is in effect (assuming another command is not failing while -e is in effect).
However, all entries in the filenamewithdomains should not start with a dot.

hergp
View Public Profile for hergp

Find all posts by hergp

#
4  

12-10-2013

wisecrackerRegistered User1,709,666Member Information Avatar
Join Date: Jan 2013Last Activity: 20 May 2020, 1:43 PM EDTLocation: Loughborough
Posts: 1,709
Thanks Given: 838

Thanked 666 Times in 467 Posts

You could try…
Code:
set +e
nslookupout=$(nslookup -type=ns $domain) || true
set -e

wisecracker
View Public Profile for wisecracker

Find all posts by wisecracker

#
5  

12-10-2013

hergpRegistered User855,198Member Information Avatar
Join Date: Jan 2010Last Activity: 17 January 2019, 3:37 AM EST
Posts: 855
Thanks Given: 37

Thanked 198 Times in 176 Posts

To remove lines with two or more dots, you can use
Code:sed ‘/..*./d’ output
class=»npb-hr»>

Last edited by hergp; 12-10-2013 at 10:56 AM..

Reason: clarification

hergp
View Public Profile for hergp

Find all posts by hergp

#
6  

12-10-2013

postcd
Banned87,2Member Information Avatar
Join Date: Oct 2013Last Activity: 1 October 2019, 4:32 AM EDT
Posts: 87
Thanks Given: 14

Thanked 2 Times in 2 Posts

Quote:
Originally Posted by hergpTo remove lines with exactly two dots, you can use
Code:sed ‘/.*..*..*/d’ output
and lines with any characters and two or more dots in it?

postcd
View Public Profile for postcd

Find all posts by postcd

#
7  

12-10-2013

hergpRegistered User855,198Member Information Avatar
Join Date: Jan 2010Last Activity: 17 January 2019, 3:37 AM EST
Posts: 855
Thanks Given: 37

Thanked 198 Times in 176 Posts

Code:sed ‘/..*./d’ output

works for more than two dots too.

hergp
View Public Profile for hergp

Find all posts by hergp

Login or Register to Ask a Question
Previous Thread
|
Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

BASH join command error PLS

i’ve tried every variation possible and keep getting not sorted error. can anyone shed any light on how to do this?

(image attached) (1 Reply)

2. UNIX for Beginners Questions & Answers

How to ignore Case with in COMM command?

Hi,

How can i ignore case between 2 files in unix using COMM command.
2 input files are:
-bash-4.1$ more x2.txt
HELLO
hi
HI
raj

-bash-4.1$ more x3.txt
hello
hi
raj

COMM command:
-bash-4.1$ comm x2.txt x3.txt
hello
HELLO
hi (3 Replies)

3. Shell Programming and Scripting

Ignore exit status for #!/bin/bash -e

I have a file /u/setuplink.txt

more setuplink.txt
ln -s /u/force.sh stopf.sh
ln -s /u/tnohup.sh tnohup.sh
ln -s /u/quick.sh startquick.sh
more runstat.sh
#!/bin/bash -e
echo «START»
/u/setuplink.txt
echo «END»
i wish to exit the runstat.sh for any errors except if the link… (2 Replies)

4. UNIX for Beginners Questions & Answers

6. Ignoring Errors in Bash

Most importantly, the whole line has an exit code of zero (success), regardless of the path in it:

$ echo bye | bash greet.sh && echo Success. || echo Failure.
Failure.
$ echo $?
0

Indeed, this comes in handy when set -e is in play, as it prevents abrupt script termination for a particular command. Note that the method only works when the commands after && and || return success. We use echo, but an alternative option for silent success is the : (null utility).

Another way of achieving the same is with the exclamation point syntax:

set -e
! echo bye | bash greet.sh
echo Success.

Executing the script above does not force an exit, despite the error in a set -e environment.

Yet another option is piping to any command that executes successfully:

set -e
echo bye | bash greet.sh | echo $?
echo Success.

Although echo $? shows 1, we still succeed, because it returns 0.

Finally, we can also turn off the -e setting before running our command, reenabling it after:

set -e
# code here
set +e
echo bye | bash greet.sh
set -e
echo Success.

In addition, we can also suppress any stdout and stderr error messages that the failing command may print:

$ badcommand
-bash: badcommand: command not found
$ badcommand >/dev/null 2>&1

Using a combination of exit code processing and ignoring the output, we can achieve a silently failing command.

All methods discussed can be applied to a whole block of code by surrounding it with curly brackets:

set -e
! {
echo First command.
badcommand
echo Third command.
}
echo Success.

Next, we’ll see some exceptional cases.

Find command with Ignore Access issues

Hi,

I am using following command to find a specific file.

find . -name «find*.txt» -type f -print

I am issuing that command at root directory since I don’t know in which sub folder that file is getting created from some other process.

As I am not having access to all directories, my… (3 Replies)

5. Shell Programming and Scripting

Bash script with awk command ERROR

Hello im new here… Im trying to read file and create folders from words in it but i get this for loop error
awk : line 3 : syntax error at or near for

my code is..

#!/bin/bash
begin
for (( i=1;i<=5;i++)); do awk -v i=$i $0 { print $i } mkdir $i done {print $i} end {} i have... (7 Replies)

8. Summary

In this article, we discussed errors in Bash.

In conclusion, Bash has a standard, but diverse error mechanism, which we can employ to handle and ignore errors.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines .

    Comments are closed on this article!

    Categories

    • Administration
    • Files
    • Filesystems
    • Installation
    • Networking
    • Processes
    • Scripting
    • Search
    • Security
    • Web

    6. Shell Programming and Scripting

    How write the ignore dupkey command in UNIX?

    I know that in oracle the is a way to write to ignore the dupkey, something like

    /*+ ignore_row_on_dupkey_index(tab, index_tab) */

    Is there a way to do the same thing but with unix commands? I think there’s a way with WHENEVER SQLERROR EXIT SQL.SQLCODE but i’m not sure and i don’t know how do… (3 Replies)

    7. Shell Programming and Scripting

    7. Remarks

    In most scenarios, we can predict with good certainty when errors are possible. Some commands, however, have inherent exceptions built-in.

    7.1. Heredoc

    One construct is using read with a heredoc :

    $ read -d » var << EOI Line one. Line two. EOI $ echo $? 1

    Error code 1? What’s the error? It turns out read returns a non-zero error code when it does not encounter a delimiter. In this case, the delimiter (-d) is an empty string (”), i.e., NUL. However, the heredoc does not end in a NUL, so it returns non-zero (fail).

    7.2. Pipefail

    Another gotcha that’s worth mentioning is the pipefail option of set. When enabled, the return code of a pipeline is the value of the last command to exit with a non-zero status. This means any failures along the pipe cause immediate termination of the pipe chain and command:

    set -e
    badcommand | echo
    # script does not exit
    set -o pipefail
    badcommand | echo
    # script exits

    Initially, piping a non-zero exit code to successful command results in overall success. After setting pipefail, the same line generates an error and forces the script to exit.

    7.3. Undefined Variables

    Yet another set flag is -u. Once we set -u, it forces any reference to an undefined variable to result in an error:

    set -e
    set -u
    echo $undefined
    # script exits

    Along with the usual ways, there is a special construct that allows us to bypass this error: ${undefined:-SOME_VALUE}. When we dereference a variable like this, it either returns the variable value or, if undefined, it returns SOME_VALUE. Consequently, we do not use an undefined value and hence to not generate an error with set -u.

    7.4. Compiling and Interpreting

    In terms of programming syntax errors, if one is encountered when processing a source code file, compilers and interpreters return error codes themselves:

    $ touch empty.c
    $ gcc empty.c
    /usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/10/../../../x86_64-linux-gnu/Scrt1.o: in function `_start’:
    (.text+0x20): undefined reference to `main’
    collect2: error: ld returned 1 exit status
    $ echo $?
    1
    $ echo [ > bad.sh
    $ bash bad.sh
    empty: line 1: [: missing `]’
    $ echo $?
    2

    7.5. Traps

    While not specifically designed to ignore errors, we can use trap to avoid abrupt exits. In a way, traps prevent or add to the default handling of an error. Without getting too much into traps, here’s a simple example script:

    set -e
    trap ‘echo «Inside trap»; echo «Line $LINENO.»‘ ERR
    echo ‘Before bad command.’
    badcommand
    echo ‘Unreachable.’

    As the output of this script shows, we can execute multiple commands before a script exits or errors out:

    $ bash script.sh
    Before bad command.
    ./script.sh: line 5: badcommand: command not found
    Inside trap.
    Line 5.

    To circumvent any of the errors in this section, we can utilize the methods we discussed in the previous one.

    Find command with ignore directory

    Dear All,

    I am using find command
    find /my_rep/*/RKYPROOF/*/*/WDM/HOME_INT/PWD_DATA -name rk*myguidelines*.pdf -print

    The problem i am facing here is find /my_rep/*/
    the directory after my_rep could be mice001, mice002 and mice001_PO, mice002_PO

    i want to ignore mice***_PO directory… (3 Replies)

    3. Error Codes

    Problem diagnosis is not straightforward. OS errors can report themselves during startup. Hardware errors might even be indicated by bad smells.

    Applications, on the other hand, have status or exit codes. Exit codes allow a user of the application to know its final status. Importantly, the widely accepted status number for no errors is 0.

    Even though errors often come with textual clarification, that is not always the case. Here are some common POSIX error codes with their descriptions:

    • EBADF=9, Bad file descriptor
    • EFAULT=14, Bad address
    • EIO=5, I/O error

    Applications can omit the description and just return the error (code).

    In essence, developers can consider and plan for different conditions and situations. However, they can rarely encompass all possible scenarios. Importantly, the OS can terminate a rogue process, which also results in an exit code.

    Errors in Bash can also result in exit codes, as we’ll see below.

    8. Shell Programming and Scripting

    bash syntax error: command not found

    I am trying to create a shell that asks the user to enter their name, and compare it to my own by saying we have the same name or saying my name and that they have a nice name too. Here is my script…

    #!/bin/bash-x
    echo «Enter your name».
    read name
    if
    then
    echo «My name is Adam too»… (1 Reply)

    9. Shell Programming and Scripting

    #!/bin/bash and #1bin/sh command not found error on mac osx terminal/shell script

    i am having a weird error on mac os x running some shell scripts. i am a complete newbie at this and this question concerns 2 scripts. one of which a friend of mine wrote (videochecker.sh) a couple weeks ago and it’s been running fine on another machine.

    then last week i wrote capture.sh and it… (2 Replies)

    10. Ubuntu

    set completion-ignore-case on doesn’t work in bash

    completion-ignore-case option doesn’t work in my version:

    /home/user $ echo $BASH_VERSION
    3.2.48(1)-release
    /home/user $ ls -l *
    -rw-r—r— 1 user user 0 2009-10-18 00:09 somefile
    -rw-r—r— 1 user user 0 2009-10-18 00:09 Somefile
    /home/user $ set completion-ignore-case on

    But when I… (2 Replies)

    Источники

    • https://www.unix.com/shell-programming-and-scripting/241905-how-ignore-error-command-bash-script.html
    • https://www.baeldung.com/linux/bash-errors
    [свернуть]
    Решите Вашу проблему!


    ×
    Adblock
    detector