让我们考虑一个例子,
scriptPath=/home/sharath/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
在上面的代码行中,如果用户是"sharath“,那么如果用户是不同的,那么他可以访问一个文件/文件夹,如何动态访问该文件夹/文件。
下面是我的below脚本(.sh文件):
#!/bin/bash
set -eu
configLocation=/etc/atollic
scriptPath=/home/sharath/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
family=STM32
arch=x86_64
version=9.2.0
configFile=${configLocation}/TrueSTUDIO_for_${family}_${arch}_${version}.properties
installPath=/opt/Atollic_TrueSTUDIO_for_${family}_${arch}_${version}/
mkdir -p /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/
tar xzf ${scriptPath}/install.data -C /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/在脚本的最后一行,${scriptPath}对于不同的用户是不同的,如何在shell脚本中处理。
更新1:
如果我使用返回"root“的${USER}或${HOME}或whoami,下面是我的日志:
tar (child): /root/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer/install.data: Cannot open: No such file or directory tar (child): Error is not recoverable: exiting now 更新2:
当前"root“中的用户
发布于 2019-02-25 06:26:47
我尝试了几种方法,最后我发现了下面的解决方案--使用下面的脚本
users
myuser=$(users)
echo "The user is " $myuser这里,用户返回当前用户名。
你的剧本变成:
#!/bin/bash
users
myuser=$(users)
set -eu
configLocation=/etc/atollic
scriptPath=/home/$myuser/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
family=STM32
arch=x86_64
version=9.2.0
configFile=${configLocation}/TrueSTUDIO_for_${family}_${arch}_${version}.properties
installPath=/opt/Atollic_TrueSTUDIO_for_${family}_${arch}_${version}/
mkdir -p /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/
tar xzf ${scriptPath}/install.data -C /opt/Atollic_TrueSTUDIO_for_STM32_x86_64_9.2.0/谢谢你回答我的问题。
发布于 2019-02-18 10:24:00
使用$HOME作为scriptPath的开头,即:
scriptPath=${HOME}/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
发布于 2019-02-18 10:25:20
您使用的Linux操作系统是什么?
您可以简单地如下所示,
scriptPath=~/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer
其中~引用用户的主目录。即/home/sarath
另一种方法是像下面这样使用它,
scriptPath="/home/whoami/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer“
https://stackoverflow.com/questions/54744871
复制相似问题