Questions tagged [case]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
4
votes
2answers
169 views

How to match a specific word or its parts in a case statement?

Suppose one has the following case: #!/bin/sh case $1 in e|ex|exa|exam|examp|exampl|example) echo "OK" ;; t|te|tes|test) echo "Also OK" ;; *) echo "Error!" ;; esac Is ...
2
votes
1answer
78 views

Executing a remote script from a code repository causes endless loop

I often execute raw versions of remote Bash scripts in GitHub with this pattern: wget -O - https://raw.githubusercontent.com/<username>/<project>/<branch>/<path>/<file> | ...
0
votes
1answer
46 views

Asterisk * is not accepting in Case statement in bash

Asterisk is not accepting in my case statement, My case statement is below, i tried escaping with \ " ',but no luck. can you correct me where i am wrong here. case $tdat in *['\*''!'@#$%^&...
1
vote
2answers
143 views

Case statement help

I am quite new to Unix and I need help with this case statement. What I want to do is to make the user select a variable with a case statement and then make the system read the result of the choice ...
0
votes
1answer
66 views

How to handle switch without argument?

I wrote a simple bash script for some USB webcam image settings and restarting of the webcamd service. At this moment, when someone uses the -t flag, they can use only "n" or "d", ...
0
votes
1answer
29 views

Is it possible to write a script that appends a “#” to any line of a file that begins with a capital letter?

In order to make a school project easier, I want to append a delimiter to each line beginning with a capital letter with a bash script. Could anyone help me out? I've tried reading the file line-by-...
-1
votes
1answer
59 views

What is the “in” in the case statement?

In a case statement like this: case $foo in bar) echo "the value of foo is bar" ;; *) echo "the value of foo is not bar" ;; esac ...
22
votes
3answers
16k views

Possible to match multiple conditions in one case statement?

I would like to do something like this where on Friday, the output is for both conditions that match: #!/bin/bash #!/bin/bash NOW=$(date +"%a") case $NOW in Mon) echo "Mon";; Tue|Wed|...
7
votes
2answers
2k views

How do I differentiate between uppercase and lowercase characters in a case statement?

I'm trying to revive my rusty shell scripting skills, and I've run into a problem with case statements. My goal in the program below is to evaluate whether a user-supplied string begins with a capital ...
3
votes
1answer
87 views

How to have a case parameter in shell scripting that is followed by a different parameter?

I want the user to type in -s followed by a number (e.g. -s 3). However, I can't seem to be passing another variable next to the existing -s. This is my code: echo choose read choice case $...
7
votes
2answers
305 views

Using a variable as pattern for case command [duplicate]

So this is my code: #!/bin/bash action_list='list|add|rem' while true; do echo "Actions include: list - show list add - add item to list ...
9
votes
2answers
636 views

POSIX catch newline in case statement

I want to catch if a variable is multiline in a case statement in POSIX shell (dash). I tried this: q=' ' case "$q" in *$'\n'*) echo nl;; *) echo NO nl;; esac It returns nl in zsh but NO ...
0
votes
1answer
82 views

BASH - case option where i can input username

I have a case script and need to input username in option -u user... how to type variable user to be able in this option list groups of specific typed user, thanks for help #!/bin/bash while [[ "$1" =...
6
votes
3answers
727 views

AND operator in case statement

I have following code. read -p "Enter a word: " word case $word in [aeiou]* | [AEIOU]*) echo "The word begins with a vowel." ;; [0-9]*) echo "The word begins with a digit." ;;...
-2
votes
1answer
259 views

using case statement to read from a variable/file while creating a MENU

read -p 'enter your choice [ 1-5 ]' choice case $choice in 1)abcrequest 2)defresponse 3)afgRequestReply 4)FSfReply 5)ghjrequest I want the case ...
2
votes
1answer
585 views

bash `case` statement to classify input as non- and integers

summary: I'd like to use a bash case statement (in other code) to classify inputs as to whether they are a positive integer a negative integer zero an empty string a non-integer string Executable ...
0
votes
1answer
141 views

nested case statement in shell script

Below is the list of files i've in my current directory: ctc.SNAPSHOT-1.0.1-abc.gz ctc.SNAPSHOT-2.0.1-bcd.gz ApprovedList.A ApprovedList.B cat ApprovedList.A BC DEF I want to pass 3 parameters to ...
-1
votes
2answers
275 views

The need for $1 and $2 for comparison with an here-string

This is a follow up to this question; I don't know why but I keep misunderstanding the following code, although I try very hard to understand it: function read_and_verify { read -p "$1:" tmp1 ...
1
vote
3answers
657 views

DOS command to Bash command conversion

I need to make Bash take DOS commands in with arguments and then convert the main command to a Bash command and still use the arguments if there are any arguments. The part I am lost on is how to ...
0
votes
1answer
49 views

same script on servers get error

I have this script on about 20 servers. CASE=$1 case $CASE in "multipath") status=$(service multipathd status | awk '{gsub(/[.]/,"");print $NF}') if [ $status = 'running' ]; then ...
1
vote
2answers
286 views

Linux - case command

How to have spaces or tabs in the menu list? PS3='Please enter your choice: ' options=("Option 1" "Option 2" "Quit") select opt in "${options[@]}" do case $opt in "...
1
vote
2answers
246 views

read-verification alternative (two prompts and if-then comparison alternative)

I am trying to create a small script for creating simple, all-default Apache virtual host files (it should be used any time I establish a new web application). This script prompts me for the domain....
7
votes
4answers
8k views

How to re-run the case statement if the input is invalid? [duplicate]

I have the following code in the middle of a script to confirm whether we want to resume the script or not. read -r -p "Would you like to continue [Y/N] : " i case $i in [yY]) ...
2
votes
1answer
2k views

Match wildcarded string in case statement using sh shell

I'm having trouble matching a wilcarded string in the sh shell. The code is part of a configure.ac so it usually gets the lamest shell available. I can't count on the Bash shell. The code first ...
12
votes
8answers
2k views

Case fallthrough based on if condition

I am looking for a way to have fallthrough happen based on an if condition within a case condition in bash. For example: input="foo" VAR="1" case $input in foo) if [ $VAR = "1" ]; then #...
6
votes
1answer
5k views

Using a variable as a case condition in zsh

My question is the zsh equivalent of the question asked here: How can I use a variable as a case condition? I would like to use a variable for the condition of a case statement in zsh. For example: ...
1
vote
2answers
1k views

compare variable with string bash

st.txt "failed" "aa" "2018-04-03T17:43:38Z" while read status name date; do case "$status" in 'aborted') echo -1 ;; "failed") echo -1 ;; '...
2
votes
1answer
581 views

how to use “joker” or wildcard in string patterns (spaces separated words) in a bash case statement?

How to use bash's "case" statement for "very specific" string patterns (multiple words, including spaces) ? The problem is: I receive a multi-word String from a function which is pretty specific, ...
-2
votes
1answer
5k views

Compare multiple string in case shell script

I am comparing string in case statement as below : input variable can be D P KL ... case $input in D|P|Q|L1|L2) val="Hello";; DD|DE|DF|CA) val="Hi" ;; MM|KL||TK|...
1
vote
3answers
13k views

Matching numbers with regex in case statement

I want to check whether an argument to a shell script is a whole number (i.e., a non-negative integer: 0, 1, 2, 3, …, 17, …, 42, …, etc, but not 3.1416 or −5) expressed in decimal (so nothing like ...
0
votes
3answers
663 views

parsing a command option in a bash script

I wrote I ‘quick’ script, and then thought of adding a “—nomail” option that would shut off my mail switch (of which the syntax is piped in from echo). I thought this would be easy but I think, ...
1
vote
3answers
3k views

Case statement allow only alphabetic characters?

case "$1" in all) echo "$1" ;; [a-z][a-z][a-z][a-z][a-z][a-z]) echo "$1" ;; *) printf 'Invalid: %s\n' "$3" exit 1 ;; esac With this the only input accepted is all, and 6 characters. It ...
1
vote
2answers
3k views

Bash scripting - exiting submenu

Running into an issue with exiting a submenu. Check it out: function submenu { select submenu1 in "Submenu 1" "Submenu 2" "Submenu 3" "Exit to main menu"; do case $submenu1 in "Submenu 1"...
4
votes
3answers
9k views

Using case and arrays together in bash

Is it possible to check if a variable is contained inside an array using case? I would like to do something like ARR=( opt1 opt2 opt3 ); case $1 in $ARR) echo "Option is contained in the ...
2
votes
2answers
2k views

Passing options/args/parameters with spaces from the script to a function within

Hello ALL and thanks in advance. I have searched the forum for my situation and have been unable to locate a solution. I've got a script that I am passing arguments/options/parameters to at the ...
0
votes
1answer
2k views

Create a menu driven script with do loop and case statement in bash

I am trying to create a bash script to create json files from a txt file in AWS. The script below is failing (I think) at the first case option. I think it's because I have the $opt variable which is ...
3
votes
4answers
3k views

Can I use comparison operators in case?

I'm building a function that will calculate the gauge of wire required given amperage, distance(in feet), and allowable voltage drop. I can calculate the "circular mils" given those values and with ...
2
votes
5answers
7k views

How to get Month in all upper case

I have two digit month value (01 to 12). I need to get the three letter month abbreviation (like JAN, FEB, MAR etc.) I am able to get it in mixed case using the following command: date -d "20170711" ...
1
vote
3answers
3k views

UNIX shell script: using “case” to validate user's input

I am trying to validate user's input So input is a number not a string or null. using case I tried to do this echo "input a number" read num1 case $num1 in "" |*[a-zA-Z]*) echo "please inter a ...
0
votes
3answers
869 views

Can a Linux 'case' statement have an 'or' concept?

Linux can use case much like many languages, so: case ${x} in one) echo "1" ;; two) echo "2" ;; esac Can you do the same with either $x or $y though? So, ...
-1
votes
1answer
2k views

Pass argument to function with case statement

I have this code on my inithome.sh: HOMEDIR="/home/$USER/" descomu () { case "$*" in -ut|--user-tar-dir) TARHOMEDIR= "$2" if test "$2" = "" then echo "The direction ...
1
vote
2answers
2k views

What does “read -n1 -s” mean in this script?

Today I have learned some tricks about menu option in command line. One of these was cat << EOF Some lines EOF read -n1 -s case $newvar in "1") echo ""; ecsa It's really magical. I ...
0
votes
1answer
478 views

Bash Script - case esac - fails

Is the following script right??? It was doing OK until yesterday! I've written the following script to help a cron job running every minute and calling some other script which may cause a command to ...
4
votes
3answers
3k views

Matching Uppercase/lowercase pairs with sed

Can I write a pattern in sed that matches patterns like Aa, Bb, Cc, etc. (i.e., Given an uppercase letter, it should match the corresponding lowercase letter) without enumerating all possibilities?
1
vote
5answers
10k views

How to use case statement to deal with multiple return values

Take a look at these attempts: $ case `true` in 0) echo success ;; *) echo fail ;; esac fail $ if `true` ; then > echo "success" > else > echo "fail" > fi success Now, why is the case ...
0
votes
1answer
663 views

check a range with switch/case in fish shell

Is something like this possible with fish? i.e. switch $var case a-z do something case "*" do something else end case "*"works fine, but case a-zdoesn't trigger. I also tried case [ a-z ...
2
votes
3answers
833 views

bash script with case statement not returning an output

I wrote this code to echo a greeting depending on what time of day it is, but when I run it it doesn't show any errors but doesn't echo anything to the command line either. To try to troubleshoot I ...
-2
votes
1answer
4k views

Unix shell scripting select and case statement

1) i want to write unix script which is like this echo "Do u want to check application logs ???" select opt in Yes No do case $opt in "Yes") commmands ;; "No") break ...
2
votes
1answer
9k views

Regex in case statement [duplicate]

I'm having a hard time getting regex matches to work in a bash case statement. Example code: #!/bin/bash str=' word1 word2' echo "With grep:" echo "$str" |grep '^\s*\&...
1
vote
3answers
167 views

case statement not behaving as expected (fuzzytime() function)

FuzzyTime() { local tmp=$( date +%H ) case $((10#$tmp)) in [00-05] ) wtstr="why don't you go to bed" ;; [06-09] ) wtstr="I see your very eager to start the day" ...