LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-26-2021, 02:39 PM   #1
PeterPanic
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Rep: Reputation: Disabled
bash command or function that doesn't change the error code


Hi!

I'm trying to write a bash function or command that will not alter the error level of the previous command.

My application: I wat to create a $PS1 that outputs the error level of the last command (green=OK=0, red=error!=0) in my prompt ($PS1) within a function or script that returns the stdout colored string, I can never manage to KEEP the error level as it was....

As soon as I create a bash function or command that sets the color to grreen/red (echo/printf), they always return "0" as their error level.

Even if I try things like that as "color.sh:
Code:
#!/bin/bash
errorLevel=$?
case "$1" in
    "black")           printf '\e[30m' ;;
    "d-red")           printf '\e[31m' ;;
    "d-green")         printf '\e[32m' ;;
    "brown")           printf '\e[33m' ;;
    "d-blue")          printf '\e[34m' ;;
    "magenta")         printf '\e[35m' ;;
    "cyan")            printf '\e[36m' ;;
    "l-gray")          printf '\e[37m' ;;
    "d-gray")          printf '\e[90m' ;;
    "red")             printf '\e[91m' ;;
    "green")           printf '\e[92m' ;;
    "yellow")          printf '\e[93m' ;;
    "blue")            printf '\e[94m' ;;
    "pink")            printf '\e[95m' ;;
    "sky")             printf '\e[96m' ;;
    "white")           printf '\e[97m' ;;
    *)                 printf '\e[m'   ;;
 esac
exit $errorLevel
Afterwards, the error level will always be "0" ...

How can I write a function or command that does not change the error level ($?)?
 
Old 05-26-2021, 04:06 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,138

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
This is tricky. A bash script runs in a new, clean bash instance so it doesn't get the $? from the caller. A function runs in the same instance as the caller, but anything you do will set $?. This includes doing:

Code:
errorLevel=$?
Fortunately, the return value of the function can be set by the bash "return" command, so this works:

Code:
function preserve() {
  errorLevel=$?
  echo $errorLevel
  return $errorLevel
}

cat bazzle
cat: bazzle: No such file or directory
preserve
1
echo $?
1
 
3 members found this post helpful.
Old 05-27-2021, 12:27 PM   #3
PeterPanic
LQ Newbie
 
Registered: Jun 2017
Posts: 4

Original Poster
Rep: Reputation: Disabled
You saved my life (... and my error level :P ). Thanks a lot!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script Read Output of One Function In Another Function bluethundr Linux - Newbie 3 08-25-2018 07:45 AM
[SOLVED] [SOLVED] bash script: can echo command, command works if I type, but command doesn't work in script ... why? hopeless_n00b Linux - Newbie 10 07-12-2018 05:57 AM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
Bash looping or function within a function FirmbIT Programming 2 04-24-2006 01:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:46 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration