我是bash编程的新手,我正在尝试设置一个bash脚本,我需要存储脚本路径的值,但正如我所看到的,它会丢失。
#!/bin/bash
dir = "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "$dir"
echo "
|------------------------------------|
|------------------------------------|
| |
|Welcome to ec2 setup!!! Let's begin |
| |
|------------------------------------|
|------------------------------------|
"
echo "
/-----------------------------
Do you want to install nodejs?
-----------------------------/"
select yn in "Yes" "No"; do
case $yn in
Yes ) echo "Enter the version number, eg; 8, 9"
read version
curl -sL https://deb.nodesource.com/setup_$version.x | sudo -E bash
sudo apt-get install -y nodejs
echo "Node version installed is $(node -v)"; break;;
No ) break;;
esac
done
echo "$dir"
echo "
/-----------------------------
Do you want to install mongodb?
-----------------------------/"
select yn in "Yes" "No"; do
case $yn in
Yes ) sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list
sudo apt-get update
sudo apt-get install -y mongodb-org
sudo service mongod start; break;;
No ) break;;
esac
done
echo "$dir"
当我运行此脚本时,echo "$dir"
不打印任何内容,除非在脚本的开头。
我在这里错过了什么?
此外,稍后在代码中,我必须更改目录,那么如何维护对初始目录的引用呢?
答案 0 :(得分:1)
变量名称和=
之间的变量声明中不能包含空格,
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)"