Find a file and set path in variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a file and set path in variable?
# 1  
Old 11-08-2016
Find a file and set path in variable?

Hi Folks -

I was wondering if you could help convert batch code in Linux? For instance, I use the following piece of code in DOS to find a file/executable, and then the FULL path as a variable.

Code:
::-- If startMaxl.exe exists, set full path --::
for %%D in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist %%D: (
    for /f "delims=" %%A in ('dir/b/s %%D:\startMaxl.exe 2^>nul') do (
        SET STARTMAXL=%%A
    )
)

Can someone help me with a similar method for Linux?

Thank you!
# 2  
Old 11-08-2016
Please explain in more detail what this does.
# 3  
Old 11-08-2016
What this does is scan all available drives on the server. I've added c-z to ensure I've captured all possibilities.

Then, when it finds the file it's searching for, it sets the path (including file name) as a variable.

Essentially I need to search the Linux environment for a file, then set that path in a variable.

Thanks!
# 4  
Old 11-08-2016
That's not a sensible concept in UNIX / Linux, or any OS which doesn't have CPM-style drive letters or completely-open file permissions. Attempting this on a UNIX system would be a recipe for disaster, migranes, and disk thrashing.

This may seem like a limitation, but it actually means, if you pick a sensible place for your executable, it won't wander and can be made the same on every machine. Linux also has easier stopgaps, like symbolic links. For that one funny machine where you can't install to /opt/myapplication/myexecutable, you could place a symbolic link or environment to the real location instead.

There were better options in DOS too, to be honest.
# 5  
Old 11-09-2016
You *could* do a

Code:
find / -name foo -type f -perm u=x

to find all occurances of a file named foo, but aside from the fact that this will likely take ages (due to the large amount of files). Also be prepared that, unless you run this as root, you will get *plenty* of error messages on stderr, because you don't have permission to dive into every directory.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-09-2016 at 04:28 AM.. Reason: Added CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Set variable to path that does not exist on local host

Can anyone suggest a workaround zone_5.org='/qaz/qwe/path/tns.osn' output /home/bingo/XXX_script.sh: line 180: zone_5.org=/qaz/qwe/path/tns.osn: no parent The path does not exist on the local machine, the allocation used to work till the server was upgraded. Red Hat Enterprise Linux... (2 Replies)
Discussion started by: squrcles
2 Replies

2. Shell Programming and Scripting

Parse output path to set variable

I am looking to parse a text file output and set variables based on what is cropped from the parsing. Below is my script I am looking to add this feature too. All it does is scan a certain area of users directories for anyone using up more than X amount of disk space. It then writes to the... (4 Replies)
Discussion started by: es760
4 Replies

3. Shell Programming and Scripting

Unable to set my PATH variable

Hello All, Hope you can understand my problem from the below code. $ cat ~/.profile PS1=`whoami`@`hostname`':$PWD $ ' export PATH="$PATH:.:/logarchive/utility/util:/usr/sbin:" $ echo $PATH /usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:.:/usr/sbin: $ echo $SHELL /usr/bin/ksh ... (6 Replies)
Discussion started by: sathyaonnuix
6 Replies

4. Shell Programming and Scripting

Find and replace string from file which contains variable and path - SH

e.g. /home/$USER/.config replace it with "" (empty) Is this possible? I think you should play a bit with sharps ## and sed:b: (2 Replies)
Discussion started by: hakermania
2 Replies

5. UNIX for Dummies Questions & Answers

PATH set but I can't find where!!!!

Hi, Can anybody help with this? When I log into my user account on my box via ssh and then instantly perform an env command I see that my path has been set as follows: PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin My user account uses the ksh shell. In my home directory there is no... (7 Replies)
Discussion started by: Donkey25
7 Replies

6. UNIX for Dummies Questions & Answers

PATH variable set incorrectly?

I've noted that in order to use commands like ifconfig, I have to prefix the commands with the directory. /etc/profile shows that the paths should be part of the PATH environment variable; any idea where the bug is? :confused: # /etc/profile # System wide environment and startup... (1 Reply)
Discussion started by: jon80
1 Replies

7. AIX

How to set path for the EDITOR variable?

For some reason something has changing in my AIX environment where when I type: ACLEDIT filename ...I get: 3002-104 acledit: EDITOR environment variable must be full pathname I know I need to reset the EDITOR variables path to /usr/bin/vi but I can't remember the syntax anyone? (2 Replies)
Discussion started by: heprox
2 Replies

8. UNIX for Dummies Questions & Answers

set variable PATH

Hi, i know that this topic discussed for many times but although i had researched them i couldnt succeed in my problem. i am following a step-by-step instruction guide and must do the following: ------------- To ensure access, set the path PATH $ORACLE_HOME/perl/bin:$PATH and set the Perl... (2 Replies)
Discussion started by: merope
2 Replies

9. UNIX for Advanced & Expert Users

How does the PATH and MANPATH environment variable get set?

Hi, How does the PATH and MANPATH environment variable get set? I want to add "/opt/SUNWspro/bin" to the search path for all the users. Where can I access this variable. I know in my home directory, depend on which shell I use, there are files such as .profile and .cshrc which I can edit to... (3 Replies)
Discussion started by: vtran4270
3 Replies

10. Shell Programming and Scripting

Set Path variable in c shell

I set my path environment variable in c shell, using the syntax below setenv PATH "${PATH}:/usr/local:/usr/local/bin" and placed this in $HOME/.login $HOME/.cshrc and /etc/.login /etc/.cshrc but when I issued echo $PATH or set command the output does not reflect changes made to... (5 Replies)
Discussion started by: hassan2
5 Replies
Login or Register to Ask a Question