LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-04-2020, 06:14 AM   #1
blason
Member
 
Registered: Feb 2016
Posts: 122

Rep: Reputation: Disabled
Can someone please help me in clearing logic in case/esac statement?


Hi All,

I am trying to write a script however stuck in my below issue and can someone please help me clearing my logic, pls?

I wanted to pass $1 and $2 flag with my script and need to take action accordingly

Here is my usage:
Quote:
if [ $# -lt 3 ];then
echo "Usage: $0 <command> <zones>"
echo "command: "
echo " -u : Update All Enabled Zones"
echo " -l : list zones"
echo " -i : Individual Update zones"
echo ""
echo "Zone names"
echo " E.g. $0 -u all | $0 -i shopping"
echo ""
exit 0
fi
now my command would be
Quote:
update -u all
update -i shopping
So I can write for $1 which is -u using case/esac but then I am sorry I have no clue about all | shopping parameter in $2.
How do I pass that?

TIA
 
Old 06-04-2020, 06:33 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
You're speaking about case statement, but where is it in your code? Could you please provide an example?
 
Old 06-04-2020, 06:46 AM   #3
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Well this is creating my confusion like I can extract the result on $1 but how about checking $2 as well along with $1?
Quote:
ase "$1" in
-u)
echo "test"
;;
i) echo "test 2"
;;
esac
 
Old 06-04-2020, 06:50 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Still don't see what your problem is.
Code:
case $1 in
-u)
  [ -n "$2" ] && echo "$2" || echo "Please provide the second argument"
;;
-i)
  [ -n "$2" ] && echo "$2" || echo "Please provide the second argument"
;;
esac
 
Old 06-04-2020, 07:01 AM   #5
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
OK - Sorry about this.

I would like to achieve below

$0 = zone-update
$1 = -u|-i|-l
$2 = all|shopping|adult|phishing

Below are the other shell scripts written
shopping-update-zone -> To update shopping zone only
adult-update-zone -> To update adult zone only
phishing-update-zone -> To update shopping zone only

zone-update -u all --> Updates all Zone - all files to update all zones like for domain in `ls *-update-zone`;do bash $domain;done

zone-update -i shopping - Code will execute and update shopping zone zone-update -i shopping-update-zone

Hope this clears the air?
 
Old 06-04-2020, 07:19 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Code:
zone_required=false
case $1 in
-u|-l) zone_required=false;;
-i)    zone_required=true;;
*) echo "Error: $1: wrong command" >&2; exit 1;;
esac

if $zone_required
then case $2 in
  all|shopping|adult|phishing);;
  *) echo "Error: $2: wrong zone" >&2; exit 2;;
  esac
fi

Last edited by shruggy; 06-04-2020 at 07:27 AM.
 
Old 06-04-2020, 07:21 AM   #7
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
hmm...thats the logic I was not able to decide..thanks let me check and verify
 
Old 06-04-2020, 08:19 AM   #8
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Somehow this is not working can you please help?

Quote:
#!/bin/bash
usage () {
echo "Usage: $0 <command> <zones>"
echo "command: "
echo " -u : Update All Enabled Zones"
echo " -l : list zones"
echo " -i : Individual Update zones"
echo " -h : help:"
echo ""
echo "Zone names"
echo " E.g. $0 -u all | $0 -i adult"
echo ""
exit 0
}
IDAM_BIN_PATH="/custom/dns/etc/sources/categoryscripts"

if [ $# -lt 3 ];then
usage
exit 0
fi

zone_need=false
case "$1" in
-l|-u|-i) zone_need=true
;;
-h|--help) usage
;;
*) echo "Error: $2: Wrong zone" >&2; exit 2
;;
esac

if $zone_need
then case "$2" in
all) for script in `ls $IDAM_BIN_PATH/idam-cat-update-*`
do
bash $script
done
;;
list) echo "Available Zones are [all|adult|blacklist|torrent|phishing|spyware|spam|dyndns|piracy|gambling|dating|social-net|ads]"
;;
adult) $IDAM_BIN_PATH/idam-cat-update-adult.sh
;;
blacklist) $IDAM_BIN_PATH/idam-cat-update-blacklist.sh
;;
torrent) $IDAM_BIN_PATH/idam-cat-update-torrent.sh
;;
phishing) $IDAM_BIN_PATH/idam-cat-update-phishing.sh
;;
spyware) $IDAM_BIN_PATH/idam-cat-update-spyware.sh
;;
spam) $IDAM_BIN_PATH/idam-cat-update-spam.sh
;;
dyndns) $IDAM_BIN_PATH/idam-cat-update-dyndns.sh
;;
piracy) $IDAM_BIN_PATH/idam-cat-update-piracy.sh
;;
gambling) $IDAM_BIN_PATH/idam-cat-update-gambling.sh
;;
dating) $IDAM_BIN_PATH/idam-cat-update-dating.sh
;;
social-net) $IDAM_BIN_PATH/idam-cat-update-social-net.sh
;;
ads) $IDAM_BIN_PATH/idam-cat-update-social-ads.sh
;;

*) echo "Error: $2: wrong zone:" >&2; exit 2
;;
esac
fi

Last edited by blason; 06-04-2020 at 08:20 AM. Reason: Sorry this is resolved...Thank you
 
Old 06-04-2020, 08:21 AM   #9
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
Issue is resolved thank you..
 
Old 06-04-2020, 08:52 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Great you found your solution, but here are couple of points:

1. Please use code tags and not quote tags as this will help maintain your formatting

2. Please enlighten everyone what the solution was so that future users who stumble on this post as being their issue can learn from you

3. Please mark as SOLVED by using the "Thread Tools" at the top of the discussion
 
1 members found this post helpful.
Old 06-04-2020, 10:23 PM   #11
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Indeed, please place your code snippets inside [CODE]...[/CODE] tags for better readability. You may type those yourself or click the "#" button in the edit controls.

And LQ is all about sharing knowledge, so posting your solution not only provides respectful feedback to those who have taken the trouble to offer help, it also is the best way of sharing knowledge you have gained with all future visitors to this thread! Please let us know how you solved it, and mark the thread as solved using the Thread Tools options above your first post.

Thanks!
 
Old 06-05-2020, 01:35 AM   #12
blason
Member
 
Registered: Feb 2016
Posts: 122

Original Poster
Rep: Reputation: Disabled
My apologies and ensure will take care of it.

Thanks for the help.
 
  


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
case esac in shell script vadirajcs Programming 16 08-10-2011 07:43 AM
case...esac how to implement in my requirement!To find the URL status. anishkumarv Linux - Newbie 5 03-12-2011 12:12 PM
Perl switch statement throwing error like Bad case statement (invalid case value?) kavil Programming 2 10-07-2010 04:50 AM
i want to make a script with case.esac Alexander.s Linux - Newbie 3 12-14-2004 05:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 AM.

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