cmd || echo "something" - doesn't exit uppon error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting cmd || echo "something" - doesn't exit uppon error
# 1  
Old 12-04-2011
cmd || echo "something" - doesn't exit uppon error

Hi guys,

I have a shell script where I have the following:

Code:
for i in ad0 ad1
do
  gpart create -s gpt $i || echo "Cannot create GPT partition on "$i". Exiting ..."
  gpart add -s 128 -t freebsd-boot $i || echo "Cannot add freebsd-boot partition on "$i". Exiting ..."
  gpart add -s 4G -t freebsd-swap -l swap $i || echo "Cannot add freebsd-swap partition to "$i". Exiting ..."
  gpart bootcode -b /mnt2/boot/pmbr -p /mnt2/boot/gptzfsboot -i 1 $i || echo "Cannot add bootcode to "$i". Exiting ..."
done

The problem is that, upon an error, the "if not" doesn't exit. Does it have something to do with the "for" loop ?
# 2  
Old 12-04-2011
You didn't ask it to exit so it is not exiting...
Code:
cmd || (echo "Not good" && exit 1)

HTH
--ahamed
# 3  
Old 12-04-2011
This should do what you want:

Code:
for i in ad0 ad1; do

  gpart create -s gpt "$i" || {
    printf >&2 'Cannot create GPT partition on "%s". Exiting ...\n' "$i"
    exit 1
    }
  
  gpart add -s 128 -t freebsd-boot "$i" || {
    printf >&2 'Cannot add freebsd-boot partition on "%s". Exiting ...\n' "$i"
    exit 1
    }
    
  gpart add -s 4G -t freebsd-swap -l swap "$i" || {
    printf >&2 'Cannot add freebsd-swap partition to "%s". Exiting ...\n'
    exit 1
    }
    
  gpart bootcode -b /mnt2/boot/pmbr -p /mnt2/boot/gptzfsboot -i 1 "$i" || {
    printf >&2 'Cannot add bootcode to "%s". Exiting ...\n' "$i"
    exit 1
    }
    
done

Something like this would be even better:

Code:
die() {
  [ -n "$1" ] &&
    printf >&2 '%s\n' "$1"
  exit 1
  }

for i in ad0 ad1; do

  gpart create -s gpt "$i" || 
    die "Cannot create GPT partition on $i. Exiting ..." 
  
  gpart add -s 128 -t freebsd-boot "$i" || 
    die "Cannot add freebsd-boot partition on $i. Exiting ..."
    
  gpart add -s 4G -t freebsd-swap -l swap "$i" || 
    die "Cannot add freebsd-swap partition to $i. Exiting ..."

  gpart bootcode -b /mnt2/boot/pmbr -p /mnt2/boot/gptzfsboot -i 1 "$i" || 
    die "Cannot add bootcode to "%s". Exiting ..."
    
done

You may need to hide the errors in a logfile, given that you want to handle the exceptions:

Code:
_errlog=${0##*/}_error.log

die() {
  [ -n "$1" ] &&
    printf >&2 '%s\n' "$1"
  exit 1
  }

for i in ad0 ad1; do

  gpart create -s gpt "$i" 2> "$_errlog" || 
    die "Cannot create GPT partition on $i. Exiting ..." 
  
  gpart add -s 128 -t freebsd-boot "$i" 2> "$_errlog" || 
    die "Cannot add freebsd-boot partition on $i. Exiting ..."
    
  gpart add -s 4G -t freebsd-swap -l swap "$i" 2> "$_errlog" || 
    die "Cannot add freebsd-swap partition to $i. Exiting ..."

  gpart bootcode -b /mnt2/boot/pmbr -p /mnt2/boot/gptzfsboot -i 1 "$i" 2> "$_errlog" || 
    die "Cannot add bootcode to "%s". Exiting ..."
    
done


Last edited by radoulov; 12-04-2011 at 04:46 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

2. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

3. AIX

echo $varibla | mail -s "subject" "xxx@xxx.com" not ruuning as expected

Hi Folks, As per the subject, the following command is not working as expected. echo $variable | mail -s "subject" "xxx@xxx.com" Could anyone figure it out whats wrong with this. I am using AIX box. Regards, (2 Replies)
Discussion started by: gjarms
2 Replies

4. Shell Programming and Scripting

Perl open(CMD, "cmd |"); buffering problem..

Hello, There's a third-party application's command that shows the application's status like "tail -f verybusy.log". When use the command, the output comes every 1-sec. but when it goes in a script below the output comes every 8-sec...What is the problem and how can I fix it? open(CMD,... (2 Replies)
Discussion started by: Shawn, Lee
2 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Shell Programming and Scripting

With that logic this echoes "echo". Question about echo!

echo `echo ` doesn't echoes anything. And it's logic. But echo `echo `echo ` ` does echoes "echo". What's the logic of it? the `echo `echo ` inside of the whole (first) echo, echoes nothing, so the first echo have to echo nothing but echoes "echo" (too much echoing :P):o (2 Replies)
Discussion started by: hakermania
2 Replies

7. Shell Programming and Scripting

Difference between using "echo" builtin and /bin/echo

So in my shell i execute: { while true; do echo string; sleep 1; done } | read line This waits one second and returns. But { while true; do /bin/echo string; sleep 1; done } | read line continues to run, and doesn't stop until i kill it explicitly. I have tried this in bash as well as zsh,... (2 Replies)
Discussion started by: ulidtko
2 Replies

8. Shell Programming and Scripting

"sed" to check file size & echo " " to destination file

Hi, I've modified the syslogd source to include a thread that will keep track of a timer(or a timer thread). My intention is to check the file size of /var/log/messages in every one minute & if the size is more than 128KB, do a echo " " > /var/log/messages, so that the file size will be set... (7 Replies)
Discussion started by: jockey007
7 Replies

9. Shell Programming and Scripting

Need help on use of "cmd" command in net::Telnet module in PERL

in "cmd" command i want to copy the ouput of the command excuted to a particular file in a directory. How to do this..?? Ex : $telnet->cmd(String => 'allip:acl=a1;',Prompt => '/</'); i want to copy o/p of the command "allip:acl=a1;" in a log file in a particular directory. Plz suggest.. (1 Reply)
Discussion started by: sudhakaryadav
1 Replies

10. Shell Programming and Scripting

assign shell var output of cmd "wc"

how do I feed output of "wc" to a shell script variable "countBcp"? (2 Replies)
Discussion started by: devy
2 Replies
Login or Register to Ask a Question

Featured Tech Videos