I created a simple bash script. The script works just fine.
When I run echo $PATH this prints my paths, I have:
/usr/local/sbin:/usr/local/bin/:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
So i moved my script to /usr/local/bin and ran chmod +x mybash.sh. I've even chmod 0777 mybash.sh
Now, when I run ./mybash.sh I just get the "No such file or directory"
Why is this happening and where's the best place to put my scripts
Once the script is in your path, you can run it just with the filename: mybash.sh rather than the path to the file: ./mybash.sh
./mybash.sh means run mybash.sh from the current folder. If you've moved mybash.sh to /usr/local/bin, then it's no longer in ./ (your current folder), so it can't find it.
Either move to /usr/local/bin to run it using ./mybash.sh or just use mybash.sh from any folder once you've moved it into a path folder.
Related
How can I change my working directory in bash script in windows. I have
~dp0 = C:\test\docker\windows and I want to change my directory to C:\test\build
So it means 2 levels up and then int o build folder
Thanks
Since C:\ is mounted by default into /mnt/c this will work.
Create a .bashrc in your home path by following command:
echo "BUILDDIR=/mnt/c/test/build" >> ~/.bashrc;source ~/.bashrc
cd $BUILDDIR
# Do your work below for example ./configure.
./configure
On my system in Git bash the C:\ root is just /c/ and other dirs from there are whatever they are, so it would be cd /c/test/build/.
You could also still say cd ../../build/.
Good luck.
To change the directory using a bash script is just like you would using normal bash.
cd "C:/test/build"
echo "You're now in the folder, do what you will."
Save the file as .sh and it can be used as such.
Note, when navigation your folders using a bash script remember the directory you'll be starting from is always the home directory.
i have a small script that runs a jar file :
#!/bin/bash
prefix="foo";
name=`ls ${prefix}*.jar`;
echo $name;
java -jar $name prop1.properties prop2.properties
when i run it in the terminal using ./myscript.sh, it works fine and the jar file executes, but when i rename it in myscript.command and double click it, i have this error :
ls: foo*.jar : No such file or directory
I saw that apparently a .command file opens a terminal at the root directory, so I tried finding the directory containing myscript.command using that :
dir = 'find <dir> -maxdepth 1 -type d -name '*myDirContainingJar*' -print -quit'
cd $dir
but dir is just blank, any ideas ???
Opening a shell script from Finder on macOS (whether it has extension .command or is an extension-less executable shell script) makes the current user's home directory the current directory (as of macOS 10.12).
To explicitly change to the directory in which the script itself is located in a bash script, use:
cd -- "$(dirname -- "$BASH_SOURCE")"
In this invocation scenario, the following POSIX-compliant variation, which uses $0 in lieu of $BASH_SOURCE, would work too: cd -- "$(dirname -- "$0")"
Note:
Generally, if your script is invoked via a symlink and you want to change to the target's directory (the actual script file's directory as opposed to the directory in which the symlink is located), more work is needed - see this answer of mine.
When opening a script from Finder, however, this issue does not apply, because Finder itself resolves a symlink to its (ultimate) target and then invokes the target directly.
The problem with your script is that it runs with a different working directory than you tested it with. When you do ls something in a script, the script looks in the current working directory (where you cded last in case you're in a terminal), not in the directory the script is in. In general, when writing scripts like this you should use the full path of the file you're referring to or figure out the directory of the script and point to the file relative to that. One solution works in Bash, but it might not in the shell you're using.
I installed root. If I use in terminal, on mac: "source /Users/student/Downloads/root-6.04.14/bin/thisroot.sh" and then "root", the program works fine. But in a tutorial I watch they say that I can write "source /Users/student/Downloads/root-6.04.14/bin/thisroot.sh" in ~/.bashrc. And then I can just type "root" and it should work directly regardless the directory I am in. But it doesn't work. What is the problem?
I'm not sure what is the equivalent directory in mac, but for example, I'm using Ubuntu.
I have:
test.sh
chmod +x test.sh (to make it executable)
then, move it to /bin/ directory
Update ~/.bashrc file, put the code at the bottom part"
export PATH=$PATH:~/bin
Finally, you can run test.sh in any directory you're in.
I'm trying to make a command to move an app I made to the applications directory without having to make a full installer. Right now this is the command I'm running with the proper permissions (I used chmod +x install.command):
mv My\ Application.app /Applications/
This gives me the output
mv: My Application.app: No such file or directory
logout
If I just run this command in the terminal it works, any idea what's going on?
EDIT:
I can't seem to manipulate "My Application.app" at all from the .command file. I tried running just rm -r "My Application.app" and it still can't seem to find the directory. The .command is in the same directory as "My Application.app"
If you aren't running this script from the dir that holds "My Application.app"
then you need to have the full path to "My Application.app".
Also you should try it with quotes.
mv "/path/to/My Application.app" /Applications/
.command files don't run with their current working directory set to the directory they are in, which is why it doesn't work; it's running in your home directory. You'll need to use either an absolute path, or a path relative to your home directory, not relative to the directory of the .command file.
I am trying to run a script from /var/www/backups/scripts and when i try tell it to zip up a file i get the below error,
I can confirm that /var/www is the home dir and that the scripts work when ran manually though putty but just not though a script.
I'm using the below code to run the zip
#!/bin/bash
unset PATH
#USER VARS
HOMEDIR=~/
BACKUP_TARG_DIR=~/sites/backups/auto
BACKUP_TEMP_NAME=tempBackupFile.tar
BACKUP_TARG_FILE=/var/www/back
DATE=`/bin/date '+%Y-%m-%d'`
echo `/bin/pwd`;
tar -zcvf test.rar /var/www/backups/scripts/tryThis
#cd /var/www
#scp "tempBackupFile.tar" 217.41.51.14:~/testfile.rar;
#tar -zcvf $BACKUP_TEMP_NAME $BACKUP_TARG_FILE;
echo "SITE-"$DATE;
below is the output i get
/var/www/backups/scripts
./autoBackup.bash: line 18: tar: No such file or directory
SITE-2011-09-05
Any one have any ideas as this is killing me, all I can think of is its something to do with where the bash script is being run from.
Why do you unset PATH ?? No wonder bash cannot execute tar.
Check your /etc/ssh/sshd_config to make sure that you don't have a chroot directory set. If you do, you will need to either create a bin directory in the chroot directory and either copy or link the necessary binaries into that directory.
Or you could always comment that line out in the config.
Either way, restart sshd and test.