What is the difference between ./myShellScript and ~/myShellScript - linux

I am beginner in learning shell script, I found a way to run the the script file from anywhere in file system with '~'
This is my shell script
myShellScript
#! /bin/bash
echo $(date): $* >> ~/notes.txt
echo $(date): $* >> ./notes.txt
And I run the ./myShellScript "write date to file"
I am trying to understand accessing file structure ./ and ~/

./ is for current directory
~/ for home directory

Related

Copy a file from a directory and paste it to multiple sub directories in linux(ubuntu) terminal?

I have a directory mnt/d/LIVI.
Inside the directory, LIVI, I have sub-directories:
mnt/d/LIVI/ak
mnt/d/LIVI/ag
mnt/d/LIVI/few
mnt/d/LIVI/ww4
mnt/d/LIVI/ks5
I wanted to copy a file named tt.txt from mnt/d/LIVI/ak/tt.txt and paste to all the sub directories of LIVI, using Ubuntu terminal. How do i do it using a shell script file?
I tried the following one, but it didn't work.
I created a text file named mnt/d/LIVI/FOLDERS.txt, This listed all the sub directories names.
And saved a script file in mnt/d/LIVI directory. The following is the script
#!/bin/sh
# -*- coding: utf-8-unix -*-
ROOTDIR=$(dirname $0 | xargs readlink -f)
for SIMDIR in cat FOLDERS.txt | xargs readlink -f ; do
cp mnt/d/LIVI/ak/tt.txt $SIMDIR
done
#cd ..
date
You may try this bash script
#!/bin/bash
cd "${0%/*}" || exit
for dir in */; do
if [[ $dir != 'ak/' ]]; then
cp ak/tt.txt "$dir"
fi
done
The script must reside under the diectory mnt/d/LIVI
Don't read lines with for.
(If you really wanted to, the syntax for that would look like
for dir in $(cat FOLDERS.txt); do
...
but really, don't. The link above explains why in more detail.)
I don't see why you want to run readlink on the directories at all?
cd "$(dirname "$0")"
while read -r dir; do
cp ak/tt.txt "$dir"
done <FOLDERS.txt
Note also Correct Bash and shell script variable capitalization

Script cannot find file

I am trying to run a script named myscript.command on a Mac/Linux machine.
#!/bin/sh
echo 'Starting'
chmod 777 ./myfile
The problem is that when I get to the chmod part I get this output:
chmod ./myfile: No such file or directory
But both myscript.command and the myfile are in the same folder.
EDIT
It seems that when I launch the script the script's location is not being preserved. How can I preserve the location?
The script is being launched via double click in the UI.
$0
In order to change the current working directory to the script's directory, put the following command right after the shebang line:
cd "$(dirname "$0")"
The $0 variable expands to the script name (path to the script), and dirname returns path to the script's directory.
Detecting the current working directory
You can use pwd command to get the current working directory. If you are actually running Bash (I'm not sure, since the shebang in your code points to /bin/sh), you can use the built-in $PWD variable:
PWD
The current working directory as set by the cd builtin.
Storing the script's path into variable
Alternatively, save the directory path into a variable, and use it in the script, e.g.:
dir="$(cd $(dirname "$0"); pwd)"
chmod 770 "$dir/somefile"
Double quotes
Note the use of double quotes. Double quotes prevent reinterpretation of special characters. It is also the way to pass strings containing spaces as a single word:
dir="some directory name"
cd "$dir"
Without double quotes the words are interpreted as separate arguments.
You might start off something like this, too..
#!/bin/sh
echo 'Starting'
if [ -f ./myfile ]; then
chmod 777 ./myfile
else
echo "File does not exist:"
ls -l
fi

Bash script that will execute a program over all files in a directory

Writing a bash script that will execute the program 'main' over all the files in the directory 'allfiles'. Main is an executable. The results are then directed to the file 'output.dat'.
Whenever I run the bash script I get the error "./main: no such file or directory". The bash script, main, and 'allfiles' directory are all in the '/home/directory/'. I'm not certain as to why i'm getting this error, or if i'm writing the bash script correctly. Any help is greatly appreciated.
#!/bin/bash
for file in /home/directory/allfiles/*
do
./main $file >> output.dat
done
edit: should clarify that 'main' is an executable file produced from a makefile
If you're not in /home/directory when you run the script, then it won't be able to find main. Try
#!/bin/bash
for file in /home/directory/allfiles/*
do
/home/directory/main $file >> output.dat
done
Additionally, if main is not executable, you'll have to get bash to call it manually. Use bash /home/directory/main $file for that, or make main executable using chmod +x main.
I prefer using find in this case of use. You can manage using subdirs ;)
#!/bin/bash
main=./main
basedir=/home/directory/allfiles/
outFile=output.dat
for file in $(find $basedir -type f)
do
$main $file >> $outFile
done

Bash script change working directory to the directory it is in

I read Can a Bash script tell what directory it's stored in?, it shows I can get script directory by DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )".
I find I can use cd command to change working directory.
This is the content of import.sh. It is in /Users/gqqnbig/SourceCode/Java/PlayerStatisticReader/bin.
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd DIR
java -cp .;jsoup-1.7.3.jar;mysql-connector-java-5.1.29-bin.jar globalVisionEntertainment.nba.Program %1
This is what I get after executing the script.
Macintosh:PlayerStatisticReader gqqnbig$ pwd
/Users/gqqnbig/SourceCode/Java/PlayerStatisticReader
Macintosh:PlayerStatisticReader gqqnbig$ bin/import.sh
: command not found 2:
: No such file or directoryDIR
: command not found 4:
I execute it in the default terminal in Macintosh.
Why is the command not found? How can I make it work?
you need to write
cd "$DIR"
strictly, you only need to add the dollar, but you should also quote the path because it may contain spaces. as to the command not found messages; I don't know. You can remove the empty lines. my guess is it's an encoding issue. do you get the "command not found" output if you paste the script directly into your terminal instead of running the file?
use the following to expand the variable
cd ${DIR}
For what it's worth, I was looking for a solution on how to schedule a command with specific work directory via crontab schedule.
I found a way to do it in one line with env.
env -C workdir - command args

How to include relative script source file bashrc

I have multiple bash file.
I want to write a master bash file which will include all required bash file in current directory.
I tried like this
#!/bin/bash
HELPER_DIR=`dirname $0`
.$HELPER_DIR/alias
But I when I put following line in my
$HOME/.bashrc
if [ -f /home/vivek/Helpers/bash/main.bash ]; then
. /home/vivek/Helpers/bash/main.bash
fi
I am getting error no such file ./alias. File alias is there. How can I include relative bash file ?
Use $( dirname "${BASH_SOURCE[0]}" ) instead.
I added these two lines two my ~/.bashrc:
echo '$0=' $0
echo '$BASH_SOURCE[0]=' ${BASH_SOURCE[0]}
and started bash:
$ bash
$0= bash
$BASH_SOURCE[0]= /home/igor/.bashrc
There is a difference between $0 and $BASH_SOURCE when you start a script with source (or .) or in ~/.bashrc.
You need to leave a space after the "dot"
. $HELPER_DIR/alias
$( dirname "${BASH_SOURCE[0]}" ) returns . if you invoke the script from the same directory or a relative path if you call it using a relative path such as ../myscript.sh.
I use script_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) to get the directory that the script is in.
Here's an example script to test this functionality:
#!/bin/bash
# This script is located at /home/lrobert/test.sh
# This just tests the current PWD
echo "PWD: $(pwd)"
# Using just bash source returns the relative path to the script
# If called from /home with the command 'lrobert/test.sh' this returns 'lrobert'
bash_source="$(dirname "${BASH_SOURCE[0]}")"
echo "bash_source: ${bash_source}"
# This returns the actual path to the script
# Returns /home/lrobert when called from any directory
script_dir=$( cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
echo "script_dir: ${script_dir}"
# This just tests to see if our PWD was modified
echo "PWD: $(pwd)"

Resources