Return from do_test if failed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Return from do_test if failed
# 1  
Old 07-29-2014
Return from do_test if failed

return from do_test if failed, not exit from bash script

Code:
do_test()
{

        get_mount_dir
        run_cmd "flash_eraseall  /dev/mtd$1"
        run_cmd "mount -t jffs2 /dev/mtdblock$1 /$mount_dir"
        run_cmd "dd if=/dev/zero of=/$mount_dir/file bs=1k count=30"
        run_cmd "umount /$mount_dir"
        
}

Code:
run_cmd()
{
        echo "$(whoami)@$(hostname):$(pwd)#$*"
        $*
}

I don't like

Code:
do_test()
{

        get_mount_dir
        run_cmd "flash_eraseall  /dev/mtd$1" || return 1
        run_cmd "mount -t jffs2 /dev/mtdblock$1 /$mount_dir" || retrun 1
        run_cmd "dd if=/dev/zero of=/$mount_dir/file bs=1k count=30" || return 1 
        run_cmd "umount /$mount_dir" || return 1 
        
}


Last edited by yanglei_fage; 07-29-2014 at 11:53 PM..
# 2  
Old 07-29-2014
I can't really see the point of this run_cmd function.

Why don't you just do something like this:

Code:
do_test()
{

        get_mount_dir
        flash_eraseall  /dev/mtd$1 &&
        mount -t jffs2 /dev/mtdblock$1 /$mount_dir &&
        dd if=/dev/zero of=/$mount_dir/file bs=1k count=30 &&
        umount /$mount_dir
}

# 3  
Old 07-29-2014
Quote:
Originally Posted by Chubler_XL
I can't really see the point of this run_cmd function.

Why don't you just do something like this:

Code:
do_test()
{

        get_mount_dir
        flash_eraseall  /dev/mtd$1 &&
        mount -t jffs2 /dev/mtdblock$1 /$mount_dir &&
        dd if=/dev/zero of=/$mount_dir/file bs=1k count=30 &&
        umount /$mount_dir
}


I modified the run_cmd to make it clear, I need it
# 4  
Old 07-30-2014
Ok that's a little better, what about this then:

Code:
do_test()
{

        get_mount_dir
        run_cmd "flash_eraseall  /dev/mtd$1" &&
        run_cmd "mount -t jffs2 /dev/mtdblock$1 /$mount_dir" &&
        run_cmd "dd if=/dev/zero of=/$mount_dir/file bs=1k count=30" &&
        run_cmd "umount /$mount_dir"
        
}

# 5  
Old 07-30-2014
Facebook

Quote:
Originally Posted by Chubler_XL
Ok that's a little better, what about this then:

Code:
do_test()
{

        get_mount_dir
        run_cmd "flash_eraseall  /dev/mtd$1" &&
        run_cmd "mount -t jffs2 /dev/mtdblock$1 /$mount_dir" &&
        run_cmd "dd if=/dev/zero of=/$mount_dir/file bs=1k count=30" &&
        run_cmd "umount /$mount_dir"
        
}



I want exit from this function if run_cmd failed, this doesn't work if

Code:
       get_mount_dir
        run_cmd "flash_eraseall  /dev/mtd$1" &&
        run_cmd "mount -t jffs2 /dev/mtdblock$1 /$mount_dir" &&
        run_cmd "dd if=/dev/zero of=/$mount_dir/file bs=1k count=30" &&
        run_cmd "umount /$mount_dir"
        xxxxx
        yyyyyyy
        run_cmd "zzzzz"

# 6  
Old 07-30-2014
OK let's change it to exit when everything works then you can handle the problem at the bottom like this :

Code:
...
run_cmd "umount /$mount_dir" && return 0

echo "Failed to erase your device"
echo "please resubmit the ....."
return 1
}

Or to deal with the failed condition and possibly continue:

Code:
...
    run_cmd "umount /$mount_dir" || {
        echo "Failed to erase the device, do you want to continue?"
        read
        case "$REPLY"
        in
           N*|n*) return 1 ;;
        esac
        echo "OK, you have chosen to continue"
    }

    rem do something else here
    ...
    return 0
}


Last edited by Chubler_XL; 07-30-2014 at 01:15 AM..
# 7  
Old 07-30-2014
If you want to have do_test return and continue running in the script at the point where do_test was called, you need to check the exit status of each call to run_cmd (such as you showed in the code you don't like, by separating calls to run_cmd with &&, or using something like Chubler_XL suggested).

If you want to exit your script if any run_cmd fails in do_test, you could add the command:
Code:
set -e

in do_test before you invoke run_cmd the 1st time and add the command:
Code:
set +e

after you invoke run_cmd the last time in do_test.

Note that all of the commands you were passing to run_cmd are simple commands ("simple" as defined by the standards). If your real code passes pipelines, lists, or compound commands to run_cmd instead of just simple commands, examine the description of what set -e does for your shell in those cases in the standards or your shell's man page.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

2. Solaris

Cygwin X Server error: xdmcp fatal error session failed session 23 failed for display

Hi, i got the following error when i tried to access the cygwin x server from a windows XP PC. "xdmcp fatal error session failed session 23 failed for display" Alternatively, when i tried to access the same Cygwin X Server from another windows XP PC which is on a different LAN... (3 Replies)
Discussion started by: HarishKumarM
3 Replies

3. UNIX for Advanced & Expert Users

Failed over crontab?

All, I am working on planning a redundant SFTP setup and some jobs are ran using cron jobs. Would there be a way to some how failover a crontab to another server? (7 Replies)
Discussion started by: markdjones82
7 Replies

4. UNIX for Dummies Questions & Answers

Command 'rm -f -r "0yfOYy-0008Nq-2j-32233-K"' failed with return code 1 and error mes

I would like to know what means this error and how to fix it Command 'rm -f -r "0yfOYy-0008Nq-2j-32233-K"' failed with return code 1 and error message Thank you (3 Replies)
Discussion started by: linuxbee
3 Replies

5. AIX

to identify failed pv

Hi friends,.... am sindhiya, i have joined as AIX level 1 support. help me to identify the failed pv in vg which has some 4 physical volumes? (2 Replies)
Discussion started by: sindhiya
2 Replies

6. AIX

SFTP Failed---Request for subsystem 'sftp' failed on channel 0

Hi, While I am trying SFTP my machine to another unix machine , it was working fine till 10 min back. But now i am getting the below error "Request for subsystem 'sftp' failed on channel 0" Could you please someone help me to solve or analyise the root cause... Cheers:b:, Mahiban (0 Replies)
Discussion started by: mahiban
0 Replies

7. UNIX for Dummies Questions & Answers

to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 's

Hi All, Can anyone please let me know the syntax / how to pick up the Return Code ( RC) from the mailx command and return it to SAS uisng 'system()' function and '${?}'. I am in a process to send the mail automatically with an attachment to bulk users. I have used 'Mailx' and 'Unencode'... (0 Replies)
Discussion started by: manas6
0 Replies

8. Solaris

Failed to power up

Dear expert, I have one server,in /var/adm/messages everyday present these messages: Jan 3 06:01:30 SERVERGINTING genunix: WARNING: Device /pci@1d,700000/pci@1/scsi@2,1/sd@5,0 failed to power up. Jan 3 06:01:31 SERVERGINTING scsi: WARNING: /pci@1d,700000/pci@1/scsi@2,1/sd@5,0 (sd45):... (2 Replies)
Discussion started by: fredginting
2 Replies

9. Programming

g++ is OK while gcc is failed

#include <iostream> using namespace std; int main(void) { cout << "hello" << endl; return 0; } I tried this: 1) g++ test.cpp -o test // It's OK 2) gcc test.cpp -o test // Failed /tmp/ccriZviL.o(.text+0x14): In function `main': : undefined reference to... (3 Replies)
Discussion started by: princelinux
3 Replies

10. Solaris

/rsc selftest failed ==> return code = 1

i edited the resolv.conf file, the i rebooted the machine, the following error appeard .. plz help /rsc selftest failed return code 1 Error ==> timeout on character recieved , uart port D FRU ==> plug-in card caller ==> warning diag-process-error rsc-ini-eth-test ... caller ==>... (1 Reply)
Discussion started by: mduweik
1 Replies
Login or Register to Ask a Question