nixCraft Linux Forum

nixCraft

Linux / UNIX Tech Support Forum

case esac reduce code ?

This is a discussion on case esac reduce code ? within the Shell scripting forums, part of the Development/Scripting category; I have this shell script : Code: Usage: $0 {all | opt1 | opt2 | opt3 | ... | opt9}" ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-06-2009, 03:05 PM
Junior Member
User
 
Join Date: Apr 2008
OS: Fedora 8
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
demuytree is on a distinguished road
Default case esac reduce code ?

I have this shell script :
Code:
Usage: $0 {all | opt1 | opt2 | opt3 | ... | opt9}"

case $1 in
all )
expression 1 //very long expression 2 //very long expression 3 //very long expression 4 //very long .... expression 9 ;;
opt1 )
expression 1 ;; //very long
opt2 )
expression 2 ;; //very long
.....
opt9 )
expression 9 ;;

In my situation, every expression is very long and have many command...

How can I reduce typing in the option 'all' so it work with all the option from 1->9->n
Reply With Quote
  #2 (permalink)  
Old 04-06-2009, 12:40 AM
Member
User
 
Join Date: May 2009
OS: Mandriva
Posts: 93
Thanks: 0
Thanked 16 Times in 16 Posts
Rep Power: 3
cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about cfajohnson has a spectacular aura about
Default


Put the code in functions:

Code:
exp1() { : whatever; }
exp2() { : whatever; }
...
exp9() { : whatever; }

case $1 in
   all) exp1; exp2; exp3; exp4; exp5
         exp6; exp7; exp8; exp9 ;;
   opt1) exp2 ;;
   ...
   opt9) exp9 ;;
esac
Reply With Quote
  #3 (permalink)  
Old 04-06-2009, 12:27 PM
Junior Member
User
 
Join Date: Apr 2008
OS: Fedora 8
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Rep Power: 0
demuytree is on a distinguished road
Default

Thanks you so much ^^!
Reply With Quote
Reply

Tags
bash , case , esac , linux , shell scripting , unix


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads

Thread Thread Starter Forum Replies Last Post
an example of wait3 and wait4 code ? demuytree Coding in General 0 16-11-2008 03:08 PM
PHP code performance zafar466 Linux software 1 22-04-2007 08:23 PM
Linux Finding Files Irrespective of Case chiku Linux software 1 16-01-2007 08:36 PM
Load OpenOffice.org in zippy i.e. reduce startup time tom Getting started tutorials 2 26-02-2006 10:36 AM
SNMP error CODE MRTG Mediocre Linux software 3 18-03-2005 01:05 PM


All times are GMT +5.5. The time now is 05:09 PM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38