Monday, April 29, 2024
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1872  / 1 Year ago, fri, november 11, 2022, 8:55:47

I would like to run a command with an argument placed in the same directory as the shell script, from a shell script.



example:



my_installer.sh:


#!/bin/sh
sudo dpkg -i --someflags=abcd blah.deb


How do I tell my_installer.sh where blah.deb is ?



I have tried ./blah.deb and looked at some env vars... I get an error about file not found.



I am assuming that the user would double-click the script, or run it from home - not cd to the script location.


More From » command-line

 Answers
4

As per the question: (a) the script is in the same directory as blah.deb, and (2) the user will run the script from some unknown other directory. In that case:



#!/bin/bash
sudo dpkg -i --someflags=abcd "${BASH_SOURCE[0]%/*}/blah.deb"


The shebang line is upgraded to bash so that we can use the BASH_SOURCE array which tells us the location of the script being executed.



Because this avoids command substitution, it should work even in the pathological case in which the directory name ends with newline characters.


[#22392] Saturday, November 12, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
neasinient

Total Points: 491
Total Questions: 120
Total Answers: 93

Location: The Bahamas
Member since Mon, Aug 2, 2021
3 Years ago
;