Skip to content
Advertisement

How to access specific path in linux using shellscript

Let us consider an example,

scriptPath=/home/sharath/Downloads/Atollic_TrueSTUDIO_for_STM32_9.2.0_installer

In the above line of code, If user is “sharath” then he can access a file/folder same way if the user is different how can access that folder/file dynamically.

below is my shellscript(.sh file):

#!/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/

In last line of the script, ${scriptPath} is diffrent for diffrent user, how can handle in shell script.

Update 1:

if i use, ${USER} or ${HOME} or whoami which returns “root” , Here is my log:

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 

Update 2:

Currently user in “root”

Advertisement

Answer

I tried with couple of way and finally i found with below solution- Use below script for the

users
myuser=$(users)

echo "The user is " $myuser

Here users returns current user name.

Your script become:

#!/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/

Thanks for answered my question.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement