I was given a task to write script that works similar to "which" command in terminal. Here is what I already wrote:
#! /bin/bash
FILE=$1
for i in $PATH
do
if [[ "$i" -eq "FILE" ]]
then
echo …
Here I need to get a full path to a file that has been found. How could i get it? Thanks in advice.
realpath
.readlink -f
.for
loop is confused. ThePATH
variable contains a single string; to loop over the individual components, you have to split it on colons. Then you have to add the name of the file you are looking for to the end of each extracted directory. The resulting paths are typically (but not necessarily) already absolute. The-eq
comparison operator is for numeric equality; use=
for string comparison. And finally use"$FILE"
with a dollar sign to examine your variable (but you should probably prefer lower case for your private variables).