0

My company has a custom tool we install in Linux to upload files to a server (among other things) called oTools. When I open a new Terminal, I can run (from any folder) oTools upload file and file is uploaded to the server.

However, the tool doesn't have an option to upload several files at once. So I'm trying to build a bash script to do that. Here is the code I built

#!/bin/sh

for file in ./folder/*
do
  oTools upload $file
done

But I'm having the following error:

./upload-all.sh: oTools: not found

I've checked my file .bashrc and it contains the path to oTools:

alias oTools="/home/user/folder/oTools-cli"

I've tried also to put the complete path on my script (the path above, that is on .bashrc file), but I'm getting the same issue. I tried doing the same code using ls instead of my installed tool and it worked...

So, how can I make bash script to recognize my custom tool?

  • 3
    Double-quote that variable "$file" when you use it. Otherwise your code will break unexpectedly when you use a filename that contains a character special to the shell. – roaima Mar 14 '18 at 21:51
2

When you write a script, it doesn't typically pull in your aliases.

You might consider specifying the command explicitly in your script:

#!/bin/sh
oTools="/home/user/folder/oTools-cli"
for file in ./folder/*
do
    ${oTools} upload "${file}"
done
5

You're facing at least two problems:

  • Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt.

  • .bashrc usually holds configuration for interactive shells, not for scripts (aka non-interactive shells).

There are some solutions:

# Set expand_aliases option and add the alias (in your script)
shopt -s expand_aliases
alias oTools="/home/user/folder/oTools-cli"

# Create a function instead of an alias (in your script)
oTools() { /home/user/folder/oTools-cli "$@"; }

# Add /home/user/folder to your PATH (in your .profile)
PATH="/home/user/folder:$PATH"
  • Some of this may fail because the OP has declared #!/bin/sh rather than #!/bin/bash. – roaima Mar 14 '18 at 21:52
  • expand_aliases I learn something useful every day.... – RonJohn Mar 14 '18 at 22:49
0

This is an issue i ran into when running scripts via cron. I am not sure if it applies here, but here try adding your path at the beginning of the script. Something like...

    #!/bin/bash

    PATH="/your/path"
    ...
  • 2
    You want to set PATH=/your/path:$PATH perhaps, or more safely PATH=/your/path:/usr/bin:/bin. Otherwise, you won't have access to standard programs (cat, ls, find, sed, awk...). – NickD Mar 14 '18 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.