For example, I have a script to create a local copy of the site (it doesn't really matter which script, I gave this script as an example)
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
VER="1.0.0"
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF
Copysite v.$VER
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [--verbose] -d ./Directory -u example.com
Program based on GNU Wget - designed to create a complete local copy of the site while maintaining the file structure
---------------------------------------
| Autor | fftcc |
| License | GNU GPL v3 |
| Website | ff99cc.art |
| E-mail | [email protected] |
| Git | codeberg.org/fftcc |
| Keyoxide | keyoxide.org/[email protected] |
---------------------------------------
Available options:
-h, --help Print this help and exit
-v, --version Print version
--verbose Print script debug info
-d, --dir Target directory (by default current directory if no parameter value is passed)
-u, --url Website address
EOF
exit
}
ver() {
cat <<EOF
$VER
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
}
msg() {
echo >&2 -e "${1-}"
}
wget_fn() {
wget --mirror -p --html-extension --base=./ -k -P "${dir-}" "${url-}"
}
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
parse_params() {
while :; do
case "${1-}" in
-h | --help) usage ;;
-v | --version) ver ;;
--verbose) set -x ;;
-d | --dir)
dir="${2-}"
shift
;;
-u | --url)
url="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${url-}" ]] && die "Missing required parameter: --url"
return 0
}
parse_params "$@"
wget_fn ${dir} ${url}
Help me write a script install.sh which would copy (or download using curl) the copysite file to the /usr/bin/ directory. But the problem is that not all systems have /usr/bin/, the paths may differ and there are such paths in the $PATH variable. But here's another problem, there may be multiple paths in $PATH. How to choose one path in which applications are installed globally for the system?
Theoretically, you can use common ones, for example, /usr/bin/ or /usr/local/bin/, but the paths in $PATH can be changed manually or by other programs. Or, for example, in thermex, the path from the root to the usr/bin directory passes through many folders /data/data/com.termux/files/usr/bin Help me find the most universal solution to the problem and write a script.
I found the following solution on the Internet: create a ~/bin in the home directory and put the script there. And add this directory to the $PATH variable via the shell profile
PATH = "$HOME/bin"
But it happens that the script will be globally executable only for the current user, which does not suit me.
If someone has the skill of creating a cli-app on node js to help me rewrite my script in js. This will ensure cross-platform compatibility via npm, I will be only too happy. Unfortunately, I have no experience in creating applications on commanderjs or oclif and do not have time to learn these tools from scratch.
CodePudding user response:
To change the path for all users you need to put your command at the bottom of the /etc/profile
directory. And you should not have spaces between the equals sign. $HOME
as well is different for each user so you shuld not use that and instead include full direct path.
# /etc/profile
export PATH="/path/to/my/file:$PATH"
Now your program should be included in the PATH
of all users.
You could also put the executable in the /bin
directory.
CodePudding user response:
I could not find a solution using $PATH. But managed to solve the issue with cross-platform and global launch from any directory using Nodejs (and with the help of kind people)
#!/usr/bin/env node
'use strict';
const commander = require('commander');
const { exec } = require("child_process");
const program = new commander.Command();
program
.version("1.0.0")
.description(`Creates a complete local copy of the site while maintaining the file structure
---------------------------------------
| Autor | fftcc |
| License | GNU GPL v3 |
| Website | ff99cc.art |
| E-mail | [email protected] |
| Git | codeberg.org/fftcc |
| Keyoxide | keyoxide.org/[email protected] |
--------------------------------------- `)
.requiredOption('-u, --url <address>', 'Website address')
.option('-p, --path <value>', 'Target directory', './');
program.parse(process.argv);
const options = program.opts();
if (options.url) {
exec(`wget --mirror -p --html-extension --base=./ -k -P ${options.path} ${options.url}`,{shell: '/usr/bin/bash'}, (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
}