Questions tagged [case]
A reserved word in shells used for running commands based on pattern matching. It must be terminated by "esac".
91 questions
2 votes
2 answers
398 views
Can I execute multiple case blocks if the pattern matches?
I want to know if there is another alternative in Bash to execute multiple blocks when the pattern matches. I don’t want to use another command, if statements, or two or more separate case blocks or ...
-1 votes
1 answer
153 views
How to specify several alternative conditions (OR operator) for single case statement? (Or Alternatively, where is shell case syntax description?) [duplicate]
I want something like this: time="1m" case "$time" in *h*) *m*) *s*) echo "handling several cases for h, m, s tokens" ;; *) echo "...
2 votes
1 answer
339 views
How is the correct syntax for a more complex case statement?
case "$1","$name" in -py | --python | --python3,*) if [[ "$name" =~ \..+$ ]]; then That doesn't catch stuff, which actually it should, like… USERNAME@HOSTNAME:~$ ...
2 votes
2 answers
656 views
For loop through a variable vector
I have a for loop and case statements. The for loop has quite a bit element list and the case statement will assign an 1D array or a vector. These values will be used in for loop after. I have the ...
0 votes
0 answers
30 views
Case Statement Not Working as Expected [duplicate]
I am attempting to write a bash script that takes input from the keyboard and displays text depending on what number was entered: #!/bin/bash read -p "Enter your age: " AGE case $AGE in ...
-2 votes
1 answer
444 views
Was `esac` intentionally `case` just in reverse? [duplicate]
I just realized that in shell scripting esac, the closing statement for case is just case reversed. This may be a stupid question but does esac actually mean something (ie an abbreviation) or was it ...
0 votes
1 answer
380 views
bash script - printing a value of an array based on the value of another array
I have two arrays and want to print a value from ARRAY2 depending on the applicable value in ARRAY1. #!/usr/bin/env bash ARRAY1=(bb.service.sql bw.service.sql) ARRAY2=(bb bw) case $ARRAY1[@] in ${...
1 vote
1 answer
128 views
Bash - How to make dynamic menu selection without eval
I'm making a script for Docker environments, and I'm a bit stuck with a pigeonhole I've gotten myself into. #!/bin/bash set -euo pipefail # Variables gituser="modem7" gitrepo="docker-...
1 vote
1 answer
80 views
Selecting from various media using awk shell script
I have made a simple backup program for my bin folder. It works. Code and resultant STDOUT below. Using rsync to copy from local ~/bin folder to a /media/username/code/bin folder. The code works fine ...
0 votes
1 answer
617 views
getopt and case function not executing
I encountered such a problem when passing a parameter to a script, the function corresponding to the case menu is not executed. The script accepts parameters as input and performs the appropriate ...
0 votes
1 answer
44 views
if condition always false despite the condition being true upon manual execution
I wish to search for a string mongo and not starting with comment i.e # in a file. homedir=`ls -d ~` echo "$homedir" if [[ `cat $homedir/2_need_softwares.txt | grep -v '^#' | grep -iq ...
2 votes
3 answers
3k views
Bash, use case statement to check if the word is in the array
I am writing a script which must accept a word from a limited predefined list as an argument. I also would like it to have completion. I'm storing list in a variable to avoid duplication between ...
2 votes
1 answer
2k views
Bash : Case statement that doesn't match a given string
I'm trying to write a case statement where the variable argument does not match a test expression. In other words: case $db in [Some Code Here]) echo "Not a valid database type&...
5 votes
3 answers
1k views
Case on multiple variables at once bash
I want to know if there is any way to case on multiple variables at once, like this: #/bin/bash arr1=(1 2) arr2=(3 4) foo=1 bar=2 case $foo && $bar in ${arr1[@]}) echo "...
0 votes
1 answer
762 views
Possible to match multiple conditions in one case statement and pass them as variables?
I have a bash script where I want to set a case variable. For file1 there are two different NUMBER variables that I then I want to pass to a text file. I have tried the following: case $FILE in &...