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
0 votes
1 answer
35 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-...
user avatar
  • 29
0 votes
1 answer
14 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|...
user avatar
  • 1,601
0 votes
3 answers
60 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:" "...
user avatar
0 votes
1 answer
22 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 ...
user avatar
0 votes
0 answers
44 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: ...
user avatar
  • 155
0 votes
1 answer
69 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 ...
user avatar
0 votes
1 answer
31 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 ;; *) ...
user avatar
1 vote
1 answer
56 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?
user avatar
  • 332
1 vote
3 answers
806 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 ...
user avatar
  • 353
0 votes
1 answer
468 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 ...
user avatar
  • 317
0 votes
2 answers
50 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 ...
user avatar
  • 47
0 votes
1 answer
65 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!" ;; *) ...
user avatar
4 votes
2 answers
242 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 ...
user avatar
2 votes
1 answer
108 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> | ...
user avatar
0 votes
1 answer
287 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 *['\*''!'@#$%^&...
user avatar
  • 183
1 vote
2 answers
187 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 ...
user avatar
  • 37
0 votes
1 answer
88 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", ...
user avatar
  • 13
0 votes
1 answer
32 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-...
user avatar
-1 votes
1 answer
66 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 ...
user avatar
  • 2,933
42 votes
3 answers
45k 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|...
user avatar
  • 15.4k
7 votes
2 answers
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 ...
user avatar
3 votes
1 answer
246 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 $...
user avatar
7 votes
2 answers
395 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 ...
user avatar
10 votes
2 answers
1k 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 ...
user avatar
  • 187
0 votes
1 answer
118 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" =...
user avatar
6 votes
3 answers
1k 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." ;;...
user avatar
  • 521
-2 votes
1 answer
427 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 ...
user avatar
2 votes
1 answer
1k 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 ...
user avatar
  • 1,163
0 votes
1 answer
223 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 ...
user avatar
  • 3
-1 votes
2 answers
516 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
880 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 ...
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 ...
user avatar
1 vote
2 answers
379 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 "...
user avatar
  • 13
1 vote
2 answers
279 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
11k 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
2 votes
1 answer
3k 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 #...
user avatar
  • 353
6 votes
1 answer
9k 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: ...
user avatar
  • 353
1 vote
2 answers
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 ;; '...
user avatar
  • 517
2 votes
1 answer
1k 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, ...
user avatar
-2 votes
1 answer
7k 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|...
user avatar
  • 57
2 votes
3 answers
17k 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 ...
user avatar
  • 179
0 votes
3 answers
838 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, ...
user avatar
  • 171
2 votes
3 answers
4k 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 ...
user avatar
  • 1,015
1 vote
2 answers
4k 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"...
user avatar
4 votes
3 answers
11k 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 ...
user avatar
2 votes
2 answers
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 ...
user avatar
  • 23
0 votes
1 answer
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 ...
user avatar
  • 621
4 votes
4 answers
4k 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 ...
user avatar
  • 32.6k
2 votes
5 answers
9k 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" ...
user avatar
  • 139