0

I have a package that comes in 2 parts - a bin file and digital certificate. This is to be distributed out to Linux sysadmins to install on RHEL systems in the enterprise. The bin file has various configuration options that can be used and I currently have them hard coded in a vary basic script that will allow them to extract the bin and cert to /tmp and run from there. The problem is that despite my instructions many sysadmins are attempting to run the package from various different locations (e.g. /home). My goal is to create a bash script that will do the following:

  1. Determine the path in which the user is attempting to launch the bin file
  2. Verify that both files exist in that path (installer.bin & cert.ssl)
  3. If both files exist launch the bin and if not display warning message

I'm still learning bash so any assistance is greatly appreciated.

  • 2
    The idea behind this site isn't to write scripts for people. It's more geared towards helping people with specific problems with their script. I'd suggest that you edit your question to include your basic script so that people can provide you with better help. – Anthony Geoghegan Apr 17 '16 at 18:17
  • 1
    This may be useful (note that unequivocally finding the path to a running bash script is not as easy as it seems): stackoverflow.com/questions/59895/… – Andrew Henle Apr 17 '16 at 19:22
1

Determine the path in which the user is attempting to launch the bin file

# this will output the path the user is in actually 
# and you can use this aswell as a variable.
echo $PWD 
# This will search the whole filesystem for your file 
# in case you need this rather than the patch the user is in.
# you can aswell make this a variable to put the whole into $()
find / -name "nameof the bin file"

Verify that both files exist in that path (installer.bin & cert.ssl

If both files exist launch the bin and if not display warning message

if [ -e filename] # when the file exists
then
    # do what you need to do
else
    # output error message
fi

See more info of the possible IF cases here.

  • 1
    echo $(pwd) emits the current working directory, which may not be where the script is located. If I start /usr/local/bin/someScript from /home/me, echo $(pwd) from within someScript will emit /home/me, not /usr/local/bin. – Andrew Henle Apr 17 '16 at 19:18
  • Revides my answer basing upon your suggestion. – Videonauth Apr 17 '16 at 19:23
  • I like the idea of using variable $(pwd) i it should be sufficient as my script will include instructions in the tarball that will specify to run from the same location where the files were extracted. Now I need to practice writing the nested 'if/then/else' statement so it will properly find both the bin and cert, write confirmation that they both exist or not, and then proceed with the installation. - Thx – Heisenberg Apr 19 '16 at 21:57
  • IF my answer was solving your problem, then you can mark it as accepted :). Wellcome to Unix & Linux – Videonauth Apr 19 '16 at 22:01
  • I really appreciate your help thus far with understanding basic bash. In my case if I wanted to check if both files exists in the present working directory, would it be best practice to nest the If statement inside a for loop? Or is there a better way of coding it when searching for multiple criteria? – Heisenberg Apr 19 '16 at 22:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.