bash error handling command not found
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and bash error handling best practices policies of this site About Us Learn more about Stack Overflow the company
Error Handling In Bash Shell ScriptBusiness Learn more about hiring developers or posting ads with us Unix & Linux Questions Tags Users Badges Unanswered bash throw error Ask Question _ Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:
Bash Error CheckingSign up Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top Catch “command not found” from shell script up vote 4 down vote favorite I have a shell script echo "Type your command" read command echo "You typed $command" $command so it's simple it runs a command.My question is command not found bash if the input is wrong suppose lw the terminal says command not found so how can I retrieve this information to my shell script and print to terminal Try again wrong command. Do I have to redirect the output of the command to a certain file and read or is there any kind of trap signal which is passed to my script.Which is your advice on how to do that it in the most efficient way. shell-script command-not-found share|improve this question asked Dec 10 '13 at 17:09 Phil_Charly 831413 Depending on things you may start by something like changing $command for $command 2>&1 | grep ": command not found" –uprego Dec 10 '13 at 17:14 @uprego : this will have a side effect of no longer displaying the normal output of 'command', and any error messages as well, as it only keeps lines containing ": commant not found" and no others. –Olivier Dulac Dec 10 '13 at 17:24 @OlivierDulac you are incorrectly assuming that the questioner is wanting to run a command that produces standard output or error. –uprego Dec 10 '
it started giving me a useful error message when I typed the name of a command that wasn't in my PATH but that was in an "sbin" directory.
Bash Script Command Not FoundMy reaction at the time was "huh, that's nice", but today I decided
Bash Command Not Found WindowsI needed a bit more information. As an example of this behavior, if I type ifconfig while not logged in command_not_found_handle as root I get the get the following: $ ifconfig Absolute path to 'ifconfig' is '/sbin/ifconfig', so running it may require superuser privileges (eg. root). Which is certainly more useful than a "command not http://unix.stackexchange.com/questions/104579/catch-command-not-found-from-shell-script found" message. Turns out that this capability is a standard feature of bash. From bash's man page: ... A full search of the directories in PATH is performed only if the command is not found in the hash table. If the search is unsuccessful, the shell searches for a defined shell function named command_not_found_handle. If that function exists, it is invoked with the original command and the original http://www.linuxjournal.com/content/bash-command-not-found command's arguments as its arguments, and the function's exit status becomes the exit status of the shell. If that function is not defined, the shell prints an error message and returns an exit status of 127. I'm not sure if that's a new feature of bash or if it's just something that's recently implemented in openSUSE. A quick grep in /etc discovered where it was happening. The function itself is in /etc/bash_command_not_found and that function gets included (if it exists) in your bash session via /etc/bash.bashrc. The function itself is not that complex but there are a couple of useful tidbits that I wanted to point out as a sidebar. The following code determines if the invoking shell was executed from Midnight Commander or is taking input from a pipe: # do not run when inside Midnight Commander or within a Pipe if test -n "$MC_SID" -o ! -t 1 ; then echo $"$1: command not found" return 127 fi And the following determines if the invoking shell is a sub-shell: # do not run when within a subshell read pid cmd state ppid pgrp session tty_nr tpgid rest < /proc/self/stat if test $$ -eq $tpgid ; then echo "$1: comma
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or http://stackoverflow.com/questions/5642521/command-not-found-error-in-bash-script posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up command not found error in bash script up vote 3 down vote favorite I have written a bash script. It basicaly gets three paths based on input parameters and then then gets the imagename/filename in command not the path. Something like: I provide: AA=/home/user Then it uses the find command to get /home/user/dir2/images/dir/tellmeimage1fun.bin Finally I have to get tellmeimage1fun.bin as output. Script: #!/bin/bash echo "arg0 n/k/d" AA=$1 CC=$3 PATH1="`find $AA/dir2/images/dir/ -name *image1*.bin`" PATH2="`find $AA/dir2/images/dir/ -name *bimage2*.bin`" PATH3="`find $AA/dir2/images/dir/ -name *cimage3*.bin`" if [ $CC = "n" ] ; then PATH=$PATH1 elif [ $CC = "k" ] ; then PATH=$PATH2 else PATH=$PATH3 fi #Getting filename name from path: IMG="`ls $PATH | cut -d "/" -f6`" OUTPUT: /users/prasapat/bin/sl5: line 22: ls: command not found command not found /users/prasapat/bin/sl5: line 22: cut: command not found If I give complete paths to ls and cut they work. But i dont want to do that for all commands in the script. If i remove the last line and echo the PATH variable it is completely fine. Only after adding the last command, I see the probelm. Kindly help and let me know if I have done any obvious error. bash share|improve this question edited Apr 12 '11 at 22:54 John Flatness 19.2k25468 asked Apr 12 '11 at 22:48 Pkp 33941226 add a comment| 5 Answers 5 active oldest votes up vote 7 down vote accepted The problem is that you are redefining the PATH variable where bash looks into to find the binary files if you don't use a complete path when calling. You should change the PATH in your bash script to MYPATH or something like that, so that it doesn't mess with the already environmental variables. If you don't know what the PATH variable is for you can look at wikipedia's article share|improve this answer answered Apr 12 '11 at 22:56 SanSS 3,7881434 Thanks a lot! Such a silly mistake!! –Pkp Apr 12 '11 at 23:55 add a comment| up vote 2 down vote The $PATH variable is a special environment variable that contains a list of directories where your shell (in this case, bash) should look in when you type a