所以我一直在开始编写 bash 脚本,并且我正在制作一个将从源代码自动安装的脚本(所以基本上它只是为你编译 tarball)。我需要它来更改目录才能转到 tarball。但是,每当我使用此命令时
read path
cd $path
Run Code Online (Sandbox Code Playgroud)
我总是收到错误tar-installer.sh: line 13: cd: ~: No such file or directory
对于任何需要它的人,这里是完整的脚本......
#!/bin/bash
# This program auto-installs tarballs for you.
# I designed this for Linux noobies who don't
# know how to install tarballs. Or, it's for
# people like me who are just lazy, and don't
# want to put in the commands ourselves.
echo "Tar Installer v1.1"
echo "Gnu GPL v2.1"
echo -n "Path to tarball:"
read path
cd $path
echo -n "Please enter the file you wish to complile..."
read file
if $file =="*.tar.gz"
then
tar -xzf $file
else
$file =="*.tgz"
then
tar -xzf $file
else
$file =="*.tar.bz2"
then
tar -xjf $file
Run Code Online (Sandbox Code Playgroud)
tarball 的最后一部分仍在进行中。但是我使用的目录cd path是~/Downloads/
这可能是一个简单的修复,但我不知道如何修复它。
您需要用~主路径替换波浪号。如果不直接执行此扩展将失败。
cd "${path/#~/$HOME}"
Run Code Online (Sandbox Code Playgroud)
将~使用 bash 的 replace替换为您的主目录${value/search_term/replacement}。
您可能还想结合 echo 和 read:
read -p "Path to tarball: " pathname
Run Code Online (Sandbox Code Playgroud)
命名变量时也要小心,比如路径(PATH 是你的环境变量)