Bash - trap error file not found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - trap error file not found
# 1  
Old 02-01-2016
Bash - trap error file not found

Hello.

In bash, is there a way to trap error "file not found" when a script call another script which is not found; then abort.

Example ( part of script running with -x option set) :

Code:
+ return 0
+ RETURN_CODE=0
+ [[ 0 -eq 0 ]]
+ /root/bin/200_yast_install/00_reset_yast_install
bash: /root/bin/200_yast_install/00_reset_yast_install: No such file or directory
<--- I would like script to abort here
+ echo + echo + [[ 1 -ne 0 ]] + echo 'RUNNING OPENSUSE : LEAP' RUNNING OPENSUSE : LEAP + [[ 0 -ne 0 ]] + echo

Any help is welcome.
# 2  
Old 02-01-2016
A trap is not necessary.
Please, add to it the following:

Code:
/root/bin/200_yast_install/00_reset_yast_install || exit 1

or add the following before executing it:
Code:
if [[ ! -x /root/bin/200_yast_install/00_reset_yast_install ]]; then
   exit 1
fi


Last edited by Don Cragun; 02-02-2016 at 12:34 AM.. Reason: Fix typo: s/instal/install/
# 3  
Old 02-06-2016
Quote:
Originally Posted by Aia
A trap is not necessary.
Please, add to it the following:

Code:
/root/bin/200_yast_install/00_reset_yast_install || exit 1

or add the following before executing it:
Code:
if [[ ! -x /root/bin/200_yast_install/00_reset_yast_install ]]; then
   exit 1
fi

Too heavy for hundreds files.
I would not use that when coding in advanced language.
This is the reason i am looking for something like TRAP.
# 4  
Old 02-06-2016
Really?
Code:
for f in /path/to/files/* ; do [ ! -x "$f" ]  && exit;done

# 5  
Old 02-06-2016
This might be a little more than you want, but have you considered using set -e? The standards describe the behavior of the shell when the set -e option is in effect as:
Code:
When this option is on, when any command fails (for any of the reasons listed in Section
2.8.1 or by returning an exit status greater than zero), the shell immediately shall exit with
the following exceptions:
    1.  The failure of any individual command in a multi-command pipeline shall not cause
        the shell to exit.  Only the failure of the pipeline itself shall be considered.
    2.  The −e setting shall be ignored when executing the compound list following the
        while, until, if, or elif reserved word, a pipeline beginning with the ! reserved word,
        or any command of an AND-OR list other than the last.
    3.  If the exit status of a compound command other than a subshell command was the
        result of a failure while −e was being ignored, then −e shall not apply to this
        command.
This requirement applies to the shell environment and each subshell environment
separately.  For example, in:
        set -e; (false; echo one) | cat; echo two
the false command causes the subshell to exit without executing echo one; however, echo
two is executed because the exit status of the pipeline (false; echo one) | cat is
zero.

# 6  
Old 02-07-2016
Did you consider the ERR trap? man bash:
Quote:
trap [-lp] [[arg] sigspec ...]
.
.
.
If a sigspec is ERR, the command arg is executed whenever a a pipeline (which may consist of a single simple command), a list, or a compound command returns a
non-zero exit status . . .
Code:
trap "echo error occurred!" ERR
ls -la dfvg
ls: cannot access dfvg: No such file or directory
error occurred!

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to skip if file not found in bash?

Hello, I have a question regarding while loop and existence of files. I am running under ubuntu 18.04 bionic, have around fifty video files inside the directory. script: for file in *.mp4 do /root/bin/ffmpeg -i $file \ -i $(basename "${file/.mp4}").bs.srt \ -i $(basename... (12 Replies)
Discussion started by: baris35
12 Replies

2. Shell Programming and Scripting

Bash to search file for string and lauch function if found

In the bash below I am searching the filevirus-scan.log for the Infected files: 0 line (in bold) and each line for OK. If both of these are true then the function execute is automatically called and processing starts. If both these conditions are not meet then the line in the file is sent to the... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Dummies Questions & Answers

File Not found - Bash script

I'm facing issues in executing the bash script of mine. This script will pick the latest file received and connects SFTP server and files is placed on this remote server. Error message Enter password: "File movement" sftp> cd Test sftp> put Test_File_201309.txt File "Test_File_201309.txt"... (6 Replies)
Discussion started by: parpaa
6 Replies

4. Shell Programming and Scripting

HOW TO - Bash REGEX - Print Error when More then One Occurence is Found Consecutively?

Hello All, Bash Version: 4.1.10(1) I'm trying to "verify" some user input. The User input will contain the "Absolute Path" a "Command" and any "Options" of that Command. For Example, say the user's input is: user_input="/usr//local/myExample/check_process -p 'java' -w 10 -c 20" I... (6 Replies)
Discussion started by: mrm5102
6 Replies

5. Shell Programming and Scripting

bash script search file and insert character when match found

Hi I need a bash script that can search through a text file and when it finds 'FSS1206' I need to put a Letter F 100 spaces after the second instance of FSS1206 The format is the same throughout the file I need to repeat this on every time it finds the second 'FSS1206' in the file I have... (0 Replies)
Discussion started by: firefox2k2
0 Replies

6. 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)
Discussion started by: amaxey45
1 Replies

7. 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)
Discussion started by: danpaluska
2 Replies

8. UNIX for Advanced & Expert Users

How to prevent grep command from throwing a system trap if No match is found.

Hi How to prevent grep command from throwing a system trap(or returning error status) if No match is found in the specified file(s) ? Consider this simple shell script: #!/usr/bin/ksh trap 'STATUS=$?;set +x;echo;echo error $STATUS at line nb $LINENO executing :\ `sed -n... (2 Replies)
Discussion started by: cool.aquarian
2 Replies

9. Shell Programming and Scripting

Bash Trap Problem?

Hey all, I'm a fairly new shell scripter (been writing some very basic stuff for a couple of years once in a blue moon). I have the need to start 2 or 3 processes from within a bash script when it's run then have the script kind of hang out and wait for the user to ctrl+c and when that happens... (3 Replies)
Discussion started by: jsabino
3 Replies

10. HP-UX

bash...Not found through where(compiling source file)

Hi i have compiled and installed bash 3.2 on my hp-ux parisc its in path /usr/local/pkg/bash/bin/bash .....When im search for this bash (through whereis bash) im not findind but other which i hve done in same procedure( gettext,m4) ..Im able to find through whereis search option can any1... (3 Replies)
Discussion started by: vasanthan
3 Replies
Login or Register to Ask a Question