Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994371
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:25:01+00:00 2026-06-15T23:25:01+00:00

I’m quite new to programming. I’m trying to create a script which I’ll run

  • 0

I’m quite new to programming. I’m trying to create a script which I’ll run on 3 servers. This script should get the hostname and then run the appropriate command. I guess I should have use ‘cases’ although I’m not sure how.
Here’s my code below.

#!/bin/bash

#Get hostname
hostname="$HOSTNAME"

PATH_DEV="some_path"
PATH_PREPROD="some_path"
PATH_PROD="some_path"

if [ $hostname == "dev" ];
then 

    find $PATH_DEV -name '*.mysql.gz' -type f -mtime +7 -exec rm -rf '{}' \;

elif [ $hostname == "prod" ];

then 

    find $PATH_PROD -name '*.mysql.gz' -type f -mtime +7 -exec rm -rf '{}' \;

elif [ $hostname == "preprod" ];

then

    find $PATH_PREPROD -name '*.mysql.gz' -type f -mtime +7 -exec rm -rf '{}' \;

else 

echo "Unknown hostname!";

fi

It’s only will run on “dev”. For the other 2 cases, it’ll return “Unknown hostname!”
What I’m doing wrong? 🙁

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:25:02+00:00Added an answer on June 15, 2026 at 11:25 pm

    Well, your main question has already been kinda answered in the comments. But as you mentioned that you probably better use case, here is the thing: Your assumption is correct, you definitely should use a case statement instead of the if–elif-construct. One of the advantages will be that matching the host name against patterns will will also solve the problem of fully qualified host names for you. Here is my solution:

    #!/bin/bash
    PATH_DEV="some_path"
    PATH_PREPROD="some_path"
    PATH_PROD="some_path"
    
    case "$HOSTNAME" in
      dev|dev.*)
        PATH_TO_USE="$PATH_DEV"
        ;;
      prod|prod.*)
        PATH_TO_USE="$PATH_PROD"
        ;;
      preprod|preprod.*)
        PATH_TO_USE="$PATH_PREPROD"
        ;;
      *)
        echo "Unknown hostname: $HOSTNAME"
        exit 1
        ;;
    esac
    find "$PATH_TO_USE" -name '*.mysql.gz' -type f -mtime +7 -exec rm -rf '{}' \;
    

    The case statement evaluates the value in $HOSTNAME and checks if it is “dev” or starts with “dev.”, if it is “prod” or starts with “prod.”, if it is “devprod” or starts with “devprod.” respectively. When a match is made, the variable PATH_TO_USE is set to the value of the corresponding PATH_XXX variable. If no specific match is found, the match-always-pattern *) will match and the script terminates with an error message (note that the error message contains the not-matching host name. This safes a lot of work sometimes 😉 ).

    If all goes well, the find command is executed at the end. (Another note: try to avoid duplication wherever you can. Doing one find command at the end allows you to change its other parameters in one place.).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I'm trying to select an H1 element which is the second-child in its group
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.