Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [case]

The tag has no usage guidance.

-1
votes
2answers
49 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
66 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
42 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
60 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
191 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....
1
vote
0answers
85 views

tail -F is not working with while and case statement [closed]

I want to monitor the logs continously and echo the output if keyword appears then continues the monitoring!! My question is how to perform the below operation Tail the logs. If keyword[down] ...
5
votes
4answers
2k 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]) ...
1
vote
1answer
408 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 ...
10
votes
8answers
1k 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 #...
5
votes
1answer
595 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
785 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
115 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
2k 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
3k views

Matching numbers with regex in case statement

I try few deferent ways I come up with (like [0-9]+ or ^[0-9][0-9]*$), and non of them works, in stead statement is matching the next case condition, *. i=1 let arg_n=$#+1 while (( $i < $arg_n ));...
0
votes
3answers
248 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
879 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
1k 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"...
2
votes
3answers
4k 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
1answer
517 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
1k 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
2k 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 ...
0
votes
5answers
3k 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
1k 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
670 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
936 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
1k 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
222 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
1k 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
4answers
4k 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
364 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 ...
3
votes
3answers
489 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
3k 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 ...
1
vote
1answer
4k 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
132 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" ...
0
votes
3answers
224 views

How to save case values in variable

selection= until [ "$selection" = "0" ]; do echo "" echo "PROGRAM MENU" echo "1 - display free disk space" echo "2 - display free memory" echo "" echo "0 - exit program" echo "" echo ...
2
votes
2answers
965 views

What is the bigger difference in this case when using case or if?

I always use if for this type of stuff, and I haven't really thought of it, but lets say something simple like this: case $1 in test) echo "test" ;; test2) echo "test" ;; test3 echo "test3" esac vs. ...
48
votes
4answers
47k views

What does “esac” mean at the end of a bash case statement? Is it required?

I have found multiple examples of "esac" appearing at the end of a bash case statement but I have not found any clear documentation on it's use. The man page uses it, and even has an index on the ...
4
votes
2answers
493 views

Case … in, file type cases

I was given homework where I need to test if $1 is a file, special file or a folder in a Case $1 In statement. I tried some things but wasn't able to make it work. Do you have any idea on how to ...
0
votes
2answers
1k views

Shell script using function () not finding command

I'm learning shell scripting and I'm studying how to use function in the shell script. The script is suppose to run as a basic math calculator with two defined numbers by the user. It's suppose to add,...
15
votes
3answers
16k views

How can I use a variable as a case condition?

I am trying to use a variable consisting of different strings separated with a | as a case statement test. For example: string="\"foo\"|\"bar\"" read choice case $choice in $string) echo "...
-1
votes
2answers
897 views

if else statement

I have a case statement inside some of the if statement, but after doing the case statement inside the if statement, the loop ends. After the case statement, it is supposed to go to the next value on ...
2
votes
4answers
6k views

How to enter the condition to check 0 or more than 0 in case

I am trying to use case to run this function if [[ $input -gt 0 || $input -eq 0 ]]; Is it possible to put in case to test the input for greater than 0 or equal to 0, or even 0 and less than 0, in ...
0
votes
1answer
64 views

Is it possible to have a case within a case, like if within a if [duplicate]

Is it possible to have a case within a case, I have seen examples of people using many ifs if ($check1=1);then if ($check2=2);then if($check3=3);then fi fi fi I'm not sure ...
2
votes
3answers
3k views

How to negate a case pattern

How add I add the condition in case whereby if it does not detect the required conditions, it will execute the command. My code: case $price in [0-9] | "." | "$") echo "Numbers, . , $ Only" ...
1
vote
2answers
601 views

How to use process substitution within a case statement without getting syntax errors?

I have a script loaded as a service in /etc/init.d/myfile When I try to start the service I get the error /etc/init.d/myservice: 21: /etc/init.d/myservice: Syntax error: "(" unexpected The issue ...
5
votes
2answers
10k views

How do you make case statements with ranges

I'm supposed to: Make a case statement that will display the a grade after input. With no loop or conditional. A=90-100, B=80-89, C=70-79, D=60-69, F=0-59 This is what I have so far: ...
2
votes
2answers
494 views

Is there a special variable containing a case statement match

If you write a Bash case statement, can you get the current match without explicitly assigning it to a variable ? Consider case $(some subshell command sequence) in one) stuff ;; *) stuff "$...
8
votes
2answers
3k views

Variable assignment outside of case statement

In many languages it is possible to assign the result of a case/switch statement to a variable, rather than repeating the variable assignment many times within the case statement. Is it possible to ...