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 9222437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:50:08+00:00 2026-06-18T03:50:08+00:00

I’m writing a bash script for a computer lab. Usually when we update the

  • 0

I’m writing a bash script for a computer lab. Usually when we update the computers we use a single image that we clone for all the computers. The problem is that all the computers get the same hostname.

So I decided to write a bash script that reads the mac address of the PC and then changes the hostname according to a table.

I’m using a case statement, but I want to use a separate config file. How can I do that?

This is the script (see the uncomfortable case):

#!/bin/bash

if [[ $(/usr/bin/id -u) -ne 0 ]]; then
    echo 'Errore, non sei root!'
    exit 1
else
        mac=$(cat /sys/class/net/eth0/address)
        basename='Lab121Pc'

        case $mac in
                'c8:9c:dc:d2:82:2c') id='01';;
                'c8:9c:dc:d2:81:f4') id='02';;
                'c8:9c:dc:d2:46:3e') id='03';;
                'c8:9c:dc:d2:82:33') id='05';;
                'c8:9c:dc:d2:81:96') id='06';;
                'c8:9c:dc:d2:83:55') id='07';;
                'c8:9c:dc:d2:2f:14') id='09';;
                'c8:9c:dc:d2:82:45') id='10';;
                'c8:9c:dc:d2:82:2f') id='11';;
                'c8:9c:dc:d2:51:2e') id='12Hp';;
                'c8:9c:dc:d2:7e:43') id='13';;
                'c8:9c:dc:d2:49:ba') id='14';;
                'c8:9c:dc:d2:81:f3') id='15';;
                'c8:9c:dc:d2:7e:3c') id='17';;
                'c8:9c:dc:d2:7e:33') id='18';;
                'c8:9c:dc:d2:83:62') id='19';;
                'c8:9c:dc:d2:4a:db') id='21';;
                'c8:9c:dc:d2:e3:d4') id='22Hp';;
                'c8:9c:dc:d2:7e:e7') id='23';;
                'c8:9c:dc:d2:b0:f4') id='24Hp';;
                'c8:9c:dc:d2:49:e8') id='25';;
                'c8:9c:dc:d2:82:31') id='26';;
                'c8:9c:dc:d2:81:2f') id='27';;
            'c8:9c:dc:d2:48:5c') id='28';;
            'c8:9c:dc:d2:81:e6') id='29';;
            'c8:9c:dc:d2:81:36') id='30';;
     esac

        hostname=$basename$id

        if [ $(cat /etc/hostname) == $hostname ]; then
                echo 'Hostname e'' gia'' corretto'
                exit 0
        else
                echo 'Hostname errato, correzzione in corso...'
                echo $hostname > /etc/hostname
                echo 'Aggiornamento riuscito'
                exit 0
        fi
fi

Thank to all.
I’m using Linux and I can’t use DHCP, but anyway thank you, it probabily will ber useful in other moments.
However, I fount the cut command, so i’v created i file containing the mac addresses:

xx:xx:xx:xx:xx:xx 01
xx:xx:xx:xx:xx:xx 02
xx:xx:xx:xx:xx:xx 03
xx:xx:xx:xx:xx:xx 04
xx:xx:xx:xx:xx:xx 05

So, I can scroll the lines of the file and then uses cut

cut -d ' ' -f 1 macs 

The delimeter is the space, so if I want the mac address I put the -f value to 1 or if I want the id to 2.

  • 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-18T03:50:09+00:00Added an answer on June 18, 2026 at 3:50 am

    If you format your file like:

    c8:9c:dc:d2:81:e6 29
    c8:9c:dc:d2:81:36 30
    

    Then you can do something like:

    id=$(grep "^$mac " | awk '{print $2}')
    

    (But I agree with the comment on your question: the best way of doing that would be through DHCP.)

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

Sidebar

Related Questions

I have a small JavaScript validation script that validates inputs based on Regex. I
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y’all
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
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am confused How to use looping for Json response Array in another Array.
I am trying to understand how to use SyndicationItem to display feed which is

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.