Questions tagged [case]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
0
votes
1answer
28 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
58 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 ...
12
votes
3answers
544 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|...
6
votes
2answers
589 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
59 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 $...
0
votes
0answers
23 views

editing a cronjob using a script [duplicate]

i am wondering if it would be possible to edit a cronjob using a script. I want to show the current cronjobs, of which the user can select a number. it will then replace the number chosen with the ...
6
votes
2answers
204 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
395 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
49 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" =...
5
votes
3answers
263 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
70 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
220 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
78 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
141 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
309 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
166 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
221 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
6k 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
1k 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
3k 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
306 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
4k 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
9k 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
537 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
2k 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
2k 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"...
3
votes
3answers
7k 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
1k 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 ...
2
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
5k 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
846 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
389 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
2k 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
8k 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
536 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
732 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 ...
2
votes
1answer
7k 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
158 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
299 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 ...
4
votes
2answers
2k 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. ...
63
votes
4answers
68k 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
804 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 ...