if/else test equivalents for case control statement in bash

if/else test equivalents for case control statement in bash



I am writing a script in bash to easily loop some software, and depending on the inputs select a different strategy to use for a particular run. For example whether or not to run on all the files in a directory or just a subset, and whether files are paired or independent. (I'm working with fastq files if you an tell)



Usually I prefer to write case control statement whenever I have 3 or more options because I find it is easier to interpret and maintain. However I want to be able to test if certain files exist similar to when if/else statements in bash do if [ -e $foo ]; then etc That got me wondering if case control statements have any equivalent test options to if/else statements example


if [ -e $foo ]; then etc



More specifically I want to know if its possible to test for the existence of a file with a case statement




1 Answer
1



if doesn't have any test options; the test command (also spelled [) does. if and case do two separate things. case compares a single value to a number of patterns to see which matches. if and elif checks the exit status of a command. It just happens that test/[ is far and away the most command command used with an if statement.


if


test


[


if


case


case


if


elif


test


[


if



Each test like -e, -d, etc, corresponds to a call to a system call named stat which returns far more information than any one test actually uses; that information is just discarded. You could run an external program wrapper around stat (also called stat) yourself manually, then pattern match on its output with a case statement. That is a little less efficient, since it involves running an external program just to access the system call. However, the bash distribution does come with a stat built-in that you could to compile and enable yourself to mitigate that problem.


-e


-d


stat


stat


stat


case


bash


stat



(The change log for bash 5 mentions a new loadable builtin for stat—among other commands—, but it doesn't seem any different than the one that I though has been available for a while.)


bash


stat



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế