Questions tagged [case]

A reserved word in shells used for running commands based on pattern matching. It must be terminated by "esac".

Filter by
Sorted by
Tagged with
2 votes
2 answers
339 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 ...
BircanA's user avatar
  • 21
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 ...
Randy Haley's user avatar
-2 votes
1 answer
96 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 ...
Kaiden Prince's user avatar
0 votes
1 answer
100 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 ${...
vrms's user avatar
  • 139
1 vote
1 answer
56 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-...
Modem7's user avatar
  • 11
1 vote
1 answer
41 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 ...
Dee's user avatar
  • 33
0 votes
1 answer
288 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 ...
Maverick's user avatar
0 votes
1 answer
20 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 ...
Ashar's user avatar
  • 459
2 votes
3 answers
2k 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 ...
vatosarmat's user avatar
2 votes
1 answer
689 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&...
Aditya K's user avatar
  • 1,950
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 "...
GamerAwesome949's user avatar
0 votes
1 answer
297 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 &...
Maria's user avatar
  • 1
0 votes
1 answer
732 views

Switch case with for loop for matching string

I am trying below thing but not able to succeed I have file with below content events_0-new events_1-new events_2-new events_3-new events_4-new events_5-new events_6-new system-events_0-new system-...
Samurai's user avatar
  • 55
0 votes
1 answer
203 views

using heredoc in case statement for remote ssh host

I have a list of ssh commands to run on remote server, so wrote this snippet. #!/usr/bin/env bash echo '' echo "WOULD YOU LIKE TO PERFORM FILES CLEANUP: " read choice case $choice in yes|...
Sollosa's user avatar
  • 1,889
0 votes
3 answers
266 views

Bash script with getopt fall into "default" the second argument

I am re-learning getopt with a tiny script in bash, but the second parameters fall into default branch of case. #! /bin/bash LONG_OPTION_LIST=( "arg-a" "arg-b:" "...
tres.14159's user avatar
0 votes
1 answer
37 views

Converting Control and Case vcf files into a format that can be processed by plink 1.9.0

I have two vcf files, one containing samples with TP53 mutations, and one containing samples with no TP53 mutations. The vcf without the mutations is the control and the vcf with the mutations is the ...
Michelle Yang's user avatar
0 votes
0 answers
55 views

function exported from .bashrc giving unexpected token for case pattern

I am using Ubuntu 18.04 LTS with Gnu Bash 5. Am getting the following error /bin/bash: printm: line 56: syntax error near unexpected token `(' /bin/bash: printm: line 56: ` +([[:digit:]]))' /bin/bash: ...
Vera's user avatar
  • 1,183
0 votes
1 answer
559 views

Returning to main menu after calling a different script

I am having a menu based script which calls for an additional shell script. The challenge is that, after the additional shell script is finished, instead of returning to the main menu, it exits ...
JugaruGabi's user avatar
0 votes
1 answer
221 views

case-substring function and quoting

i use a simple function in a script in order to ckeck if a string contains a particular substring: #!/bin/bash # subs() { case $2 in *$1*) return 0 ;; *) ...
noemata's user avatar
1 vote
1 answer
105 views

Changing the case of lines in ed

Can I employ the tr command to change the case of a line or range of lines while working in ed? Or is there another way?
Edman's user avatar
  • 482
1 vote
3 answers
2k views

Matching a valid version number inside case statement

I want to match a version number inside a case statement. The version number could look like this 1.12.0.32 1.12.0.32.1 2.10.0.30.1.2 and is stored inside a shell variable: version. As I searched on ...
ashish_k's user avatar
  • 375
0 votes
1 answer
2k views

syntax error near unexpected token `)' when using case command

I am parsing option arguments to a bash function, where one of the options allows an optional arguments. Bash has started to complain about an unexpected token `)', but cannot see clearly what's wrong ...
Pietru's user avatar
  • 371
0 votes
2 answers
56 views

how can I check/test 2 conditions then execute a specific list of commands I tried case, I tried if, I tried for not working

sorry if it was confusing. here is what the script needs to be doing I input 2 values. DB Server name= 192.168.1.1 DB schema = My_DB_schema Then I need a method to execute some command when those 2 ...
Nick G.'s user avatar
  • 47
0 votes
1 answer
334 views

a simple case/esac construct exercise fails, why?

This is the BASH script: #!/bin/bash read -p "Type in a color name, please: " COLOR case "$COLOR" in green | red | yellow) echo -n "nice colors!" ;; *) ...
WobblyWindows's user avatar
5 votes
2 answers
1k 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 ...
user464484's user avatar
2 votes
1 answer
297 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> | ...
variable_expander's user avatar
0 votes
1 answer
729 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 *['\*''!'@#$%^&...
Renga's user avatar
  • 393
1 vote
2 answers
542 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 ...
Filippo's user avatar
  • 37
0 votes
1 answer
332 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", ...
GohanCZ's user avatar
0 votes
1 answer
41 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-...
forrestal's user avatar
0 votes
1 answer
89 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 ...
Toothrot's user avatar
  • 3,255
57 votes
3 answers
83k 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|...
MountainX's user avatar
  • 17.3k
7 votes
2 answers
3k 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 ...
John Jones's user avatar
3 votes
1 answer
863 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 $...
MargaritaK's user avatar
7 votes
2 answers
591 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 ...
user361323's user avatar
10 votes
2 answers
2k 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 ...
aaa's user avatar
  • 207
0 votes
1 answer
178 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" =...
Šimon Drienik's user avatar
6 votes
3 answers
3k 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." ;;...
smc's user avatar
  • 571
-2 votes
1 answer
737 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 ...
Shivam Malhotra's user avatar
3 votes
1 answer
2k 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 ...
TomRoche's user avatar
  • 1,275
0 votes
1 answer
387 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 ...
us3r's user avatar
  • 3
-1 votes
2 answers
876 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 ...
user avatar
1 vote
3 answers
1k 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 ...
Trinity Zamrzla's user avatar
0 votes
1 answer
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 ...
BlackCrystal's user avatar
1 vote
2 answers
535 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 "...
manuelc's user avatar
  • 13
1 vote
2 answers
380 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....
user avatar
8 votes
4 answers
13k 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]) ...
user avatar
3 votes
1 answer
4k 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 ...
user avatar
13 votes
8 answers
3k 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 #...
Smashgen's user avatar
  • 383
9 votes
1 answer
14k 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: ...
Smashgen's user avatar
  • 383