Find the answer to your Linux question:
Results 1 to 2 of 2
Hi, I need to modify some text in a sh file as follows and am not sure about the syntax. I have read thru a few tutorials, but I haven't ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    3

    Question bash script text manipulation

    Hi,
    I need to modify some text in a sh file as follows and am not sure about the syntax. I have read thru a few tutorials, but I haven't found what I need.

    MY_HOSTNAME=`/bin/hostname -s`
    MY_NFS1=$MY_HOSTNAME + .bw.mycampus.edu
    MY_DHCPD1=$MY_HOSTNAME + .bw.mycampus.edu
    MY_TFTP1=$MY_HOSTNAME + .bw.mycampus.edu

    where the resulting environment variables will be:

    MY_HOSTNAME=myhostname (without fqdn)
    MY_NFS1=myhostname.bw.mycampus.edu
    MY_DHCPD1=myhostname.bw.mycampus.edu
    MY_TFTP1=myhostname.bw.mycampus.edu

    I realize that this is probably really easy but I am new to scripting and not sure how to append the text as needed.

    Thanks!
    gabrielle64

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    As you've probably seen, Bash takes most of the variable declaration to be rather literal. Following that thought, what you want is:
    Code:
    MY_HOSTNAME=`/bin/hostname -s`
    MY_NFS1=$MY_HOSTNAME.bw.mycampus.edu
    MY_DHCPD1=$MY_HOSTNAME.bw.mycampus.edu
    MY_TFTP1=$MY_HOSTNAME.bw.mycampus.edu
    In this case, it works because '.' is an invalid character in a variable name, so it knows to stop treating this like a variable. Let's suppose that we wanted to instead do $MY_HOSTNAME and then 'hello'. We would do:
    Code:
    ${MY_HOSTNAME}hello
    Here, we specifically tell it how much of this is the variable name.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...