Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Script: If Hostname as the same name than the folder
16-01-2015, 11:39 AM,
#1
Script: If Hostname as the same name than the folder
Hello everybody!

I'm here for my last question maybeSad.

So before I can deploy my Raspberry on our office I need to make a script who can see if the name of my rpitc is the same than a config file. So i made some test for doing this. I'm really lost, I don't know how I can see if my file have the same name than my rpitc.

So this is my test code:
Code:
do_start()
cd /home/rpitc
if [$HOSTNAME=./$HOSTNAME];
then
printf '%s\n' "Worked!"
cd;
else
printf '%s\n' "God Damnit! How did you get that?"
cd;
fi

I got a syntax error with "./" before the HOSTNAME. In my final script I need to see if the hostname is different from the name of the ica file, he delete the current file and replace him by the right ica file. I don't know if it's clear Huh.

With the final script, I need to get this:
Code:
-If the Hostname is different from the file name
          -Delete and replace him with my tftp protocol
-If the Hostname is the same than the file name
          -Launch my citrix with the right ica file

So in the final version of my script the file can be named "hostname.ica"Sad

But for this moment I need to know how i can see if the Hostname is different from the name of my file.
Reply
16-01-2015, 12:42 PM,
#2
RE: Script: If Hostname as the same name than the folder
Hi Arag Mond,

I use a script in my office that auto connects our Pi Thin Clients to a Windows VM. I do this comparing the hostname of my Pi to a list of hostnames in a config file...perhaps this script will be of use to use.

I've posted it here in this link (General Discussion) - http://forum.armtc.net/showthread.php?tid=1461

In this way I have a golden image of rpitc I've created, I simply give each new Pi Thin Client a static IP and hostname and my scripts take over and auto connect for me.

I hope this helps.
Reply
16-01-2015, 01:08 PM,
#3
RE: Script: If Hostname as the same name than the folder
That's nice but in my office we are in DHCP, and I'm just in training in this enterprise. That's why I need to only use Hostname. But thanks for your answer!
Reply
16-01-2015, 02:01 PM,
#4
RE: Script: If Hostname as the same name than the folder
Well, my bash foo is pretty weak but give this a try (untested):

Find the name of your file using something like this:

Code:
dest="/home/rpitc/scripts/mysql.tgz"
## get file name i.e. basename such as mysql.tgz
findfile="${dest##*/}"
## display just the filename
echo "${findfile%.*}"

I save my Pi hostname to a variable like so:

Code:
ThisHost=$(hostname)

Now do the check to see if it matches:

Code:
if [ "$ThisHost" == "${findfile}" ]
    then
       <Do some stuff>
    fi

Hopefully this will get you started...good luck!
Reply
16-01-2015, 03:45 PM,
#5
RE: Script: If Hostname as the same name than the folder
Your script don't do what I want.
My destination file need to change if my hostname change. I need to take the file who have the same name than my host.

I had try something, I think it's working but I have an access denied (in root mode).

Code:
do_start()
cd /home/rpitc
host= "$HOSTNAME"
file= "/home/rpitc/"
if [ -f $file$host];
then
printf '%s\n' "Worked!"
else
printf '%s\n' "God Damnit! How did you get that?"
fi

And I have this error:
Code:
/etc/init.d/ske: 30: /etc/init.d/ske: /home/rpitc/: Permission denied
Worked!
/etc/init.d/ske: 29: /lib/init/init-d-script: : Permission denied

So I can see my beautifull "Worked!", but I had never see this... Permission denied in root is very new for me Huh.
So you have a solution for this?
Thanks for your help greavette!
Reply
16-01-2015, 07:41 PM,
#6
RE: Script: If Hostname as the same name than the folder
Ok...let's see what I can do...give me more details please:
  • You have a config file in /home/rpitc correct? What is the extension of this config fie? Is it .ica?
  • How many config files will you have in the folder you are searching? One or many?
  • Please explain more why you might have a config file different from the hostname in the first place. Are you starting with a general name for this config file on your base (I call golden) image of rpitc and then flashing this base image to an SD Card whereupon first boot of the new thin client workstation you will change the Hostname for that workstation?
  • What's in this config file. Besides renaming the config filename don't you need to change the details within the config file to make the connection work?

Thanks.
Reply
18-01-2015, 04:01 PM,
#7
RE: Script: If Hostname as the same name than the folder
Give this bit of code a try. I've included a lot of displays and examples of how to show your file (with and without extension). It will save your hostname to a variable, search a folder of your choosing and report back on what it finds.

Be sure to update all the paths to your file location and don't forget to make the sh file exectuable:

Code:
chmod +x chkscript.sh

Then run it using this:

Code:
sh ./chkscript.sh


Let me know if this works for you or not.

Code:
#!/bin/bash

# Creating my logfile variable:
Logfile=/home/rpitc/scripts/test/chkscript.log

#Creating and checking my hostname variable:
ThisHost=$(hostname)
echo "This is my hostname check:"
echo $ThisHost

#This will find the file in the /scripts/test folder and save it to a variable:
dest=$(find /home/rpitc/scripts/test -name "*.config")
#echo "This is my dest check:"
#echo $dest
findfile="${dest##*/}"
echo "This is my findfile check with extension:"
echo $findfile
echo "This is my findfile check WITHOUT extension:"
echo "${findfile%.*}"

# If check to see if my hostname $ThisHost matches the file  $findfile:
if test "$ThisHost" = "${findfile%.*}"
then
    echo "$(date "+%m%d%Y %T") : hostname matches my file" >> $Logfile
    echo " " >> $Logfile
    exit 0
else
    echo "$(date "+%m%d%Y %T") : hostname DOES NOT match my file" >> $Logfile
        mv /home/rpitc/scripts/test/$findfile /home/rpitc/scripts/test/$ThisHost.config
        dest=$(find /home/rpitc/scripts/test -name "*.config")
        echo "This is my dest check:"
        echo $dest
        findfile="${dest##*/}"
        echo "This is my findfile check with extension:"
        echo $findfile
        echo "This is my findfile check WITHOUT extension:"
        echo "${findfile%.*}"
    exit 0
fi
Reply
19-01-2015, 10:27 AM,
#8
RE: Script: If Hostname as the same name than the folder
For this moment I just have a file named Host in my /home/rpitc.
But for my final script I need to use an ica file.

I just search one file but I have two file in my folder they are called "Host" and "NoHost", and my script need to search the file named "Host".

Yea when I put the image for the first time on a raspberry I change my hostname and my script search automatically the good ica file. I need to have a different ica file each user because I need to put their Username and their ClientName.

I got an error with your code:
Code:
/etc/init.d/script.sh: 39: /etc/init.d/script.sh: {find: not found
This is my findfile check with extension:
$ {dest##*/}
This is my findfile check WITHOUT extension:
$ {dest##*/}
/etc/init.d/script.sh: 55: /etc/init.d/script.sh: Bad substitution
Reply
19-01-2015, 02:54 PM, (This post was last modified: 19-01-2015, 03:51 PM by greavette.)
#9
RE: Script: If Hostname as the same name than the folder
That's strange..I tested this script multiple times on my rpitc and it worked perfectly. Could you post your script please so I can see what it looks like?

I see you are running this script from /etc/init.d/...before running it there try a test of running my script from a home folder where you don't need sudo access to and all. Changee an .ica file to be your hostname and make sure the script works.

Thanks.

Just thought of something. You said you have two files, Host and NoHost. Do they both have the same file extension?

In my script I assumed only one file (where in my test I was looking for *.config...hopefully you changed this to the extension you need to see).

So for your task, which one do you change? The Host or the NoHost file? If you only need one file (which you are changing the name of) then why do you need two?

For my script I assumed you didn't know the name of your file (which is why I derived the name from my code:

Code:
dest=$(find /home/rpitc/scripts/test -name "*.config")

But if you know the name you want to change already then the script becomes much easier. If your file is always called Host (or NoHost) then simply put that file into a variable, then do your if check on does this file match the variable for your hostname. If it fails do the mv (move) statement which renames the file.

Makes sense?
Reply
19-01-2015, 03:54 PM, (This post was last modified: 19-01-2015, 04:01 PM by Arag Mond.)
#10
RE: Script: If Hostname as the same name than the folder
Okay so this is my script:

Code:
do_start()
#Creating my log file variable:
Logfile=/home/rpitc/ske.log

#Creating and checking my hostname variable:
ThisHost=$(hostname)
echo "This is my hostname check:"
echo $ThisHost

#This will find the file in the /home/rpitc folder and save it to a variable:
dest=$(find /home/rpitc/ -name "*.ica")
echo "This is my dest check:"
echo $dest
findfile="${dest##*/}"
echo "This is my findfile check with extension:"
echo $findfile
echo "This is my findfile check WITHOUT extension:"
echo "${findfile%.*}"

#If check to see if my hostname $ThisHost matches the file $findfile:
if test "$ThisHost" = "${findfile%.*}"
then
   echo "${date "+%m%d%Y %T"} : hostname matches my file" >> $Logfile
   echo " " >> $Logfile
   exit 0
else
   echo "${date "+%m%d%Y %T"} : hostname DOES NOT matches my file" >> $Logfile
   mv /home/rpitc/$findfile /home/rpitc/$ThisHost.ica
   dest=$(find /home/rpitc -name "*.ica")
   echo "This is my dest check:"
   echo $dest
   findfile="${dest##*/}"
   echo "This is my dest check:"
   echo $findfile
   echo "This is my findfile check WITHOUT extension:"
   echo "${findfile%.*}"
  exit 0
fi

In the future we gonna create 300 ICA files. Each user have his file. So when you change the hostname of your rpitc the script take the file who have the same name than the Hostname. So I need two file to make this test. So when I change my Hostname in "foo" the script go take the "foo.ica" and if I change my Hostname in "drake" the script go take the file "drake.ica".

I don't really know why it's not working... And really thanks for your help because I'm not very good for that.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)