Run bash script as if I it was executed from a different directory - linux

Maybe the title is a bit "stupid" but I do not know how to express my question and how to search for the question also, even if it is something very simple.
I have a set of scripts that produce a set of reports in the folder they are executed by. For example I have the script "my_script.sh" in the folder /a/folder/ and in this folder a set of output is stored. Since I have a lot of experiments that I want to let them run for the whole week I was thinking of creating a bash script that will call all the other scripts.
But the output will be stored in the folder that the global script is present.
For example:
/global/folder/global_script.sh
---> All the output is stored in this folder.
The global_script.sh may contain something like this:
/experiments/exp1/script1.sh >report1.txt
/experiments/exp1/script2.sh >report2.txt
/experiments/exp1/script2.sh >report3.txt
And I want the output of the bash scripts to be in their folder and not in the global folder.
Currently I am doing this manually navigating to the folder and executing the script.
(Ok I can change the code and use absolute paths! but is any better way to do that? )

you could change the working directory before you execute each script, or redirect the output to the directory you want:
cd /experiments/exp1/
sh /experiments/exp1/script1.sh >report1.txt
or
sh /experiments/exp1/script1.sh > /experiments/exp1/report1.txt

What's wrong with simply changing directory?
cd /experiments/exp1
./script1.sh >report1.txt
./script2.sh >report2.txt
./script2.sh >report3.txt

Related

In my shell script I would like to list all the files in my current directory

In my shell script I would like to list all the files and directories in my current directory.
I know the command is ls, but I have no idea how to run it in a shellscript.
Thanks for anyhelp.
So you're basically asking: what is a shell-script, how do I create one, and how do I run it ....
Use your editor of choice to create a file, give it the following content:
#!/bin/sh # change to your preferred shell, sh being a low common denominator
command # in your immediate question that would be `ls`
Save the file.
Run chmod u+x file (not the word file, but what you called your saved script).
Then you can execute your file like so:
./file

Owner cannot create file in folder with all permission Linux

I'm trying to create few files under folder with script, but not able to.
following is the folder privilege under which some files are to be created
su pnaid
The partial script content, responsible to create folder and create files in it
MKDIR_CMD="/bin/mkdir -p \"${PATH_TO_WRITE}\" > /dev/null 2>&1"
"${MKDIR_CMD}"
echo "Checking Dir Path exist"
if [ -d "${PATH_TO_WRITE}" ]; then
echo "Calling another script to create files under this folder"
"/createFiles.sh \"${PATH_TO_WRITE}\""
else
echo "WARNING: unable to create folder"
The parent folder to the $(PATH_TO_WRITE) has following privileges
drwxr-x---. 2 pnaid pnaid 4096 Dec 3 12:31 work_directory
Each time the statement "WARNING: unable to create folder" is displayed.
I tried creating a folder with pnaid user having 777 permission and feeding that in script instead of "${MKDIR_CMD}", in that case the statement "Calling another script to create files under this folder" is displayed but the other script is not able to write to this folder.
Also the echo statements from createFiles.sh when called from original script are not displayed, is there any way to view it.
If we perform the same commands on shell prompt instead of script, the commands work and desired output is obtained; i.e. folder is created with all the files in it.
Also the same script works if we run it with user root.
I believe this should work across Linux flavors, in this case I'm using CentOS
Please help me resolve this issue and let me know if I have missed mentioning any details.
Thanks
This line:
"${MKDIR_CMD}"
will not work. It treats the entire value of $MKDIR_COMMAND as the name of the program to run, it doesn't split it into the program and arguments, because you put quotes around it. Also, redirections are not processed when expanding a variable.
You need to use eval to re-parse the string:
eval "$MKDIR_CMD"
You have the same problem with:
"/createFiles.sh \"${PATH_TO_WRITE}\""
This should be:
/createFiles.sh "$PATH_TO_WRITE"
These problems don't depend on permissions, I doubt the script really works when run as root.
Here's a related question that shows how to store command parameters best in variables:
Setting an argument with bash
However, the solution there (using an array instead of a string) won't work if you're also storing shell operators like redirection.

Add a bash script to path

I want to add a small script to the linux PATH so I don't have to actually run it where it's physically placed on disk.
The script is quite simple is about giving apt-get access through a proxy I made it like this:
#!/bin/bash
array=( $# )
len=${#array[#]}
_args=${array[#]:1:$len}
sudo http_proxy="http://user:password#server:port" apt-get $_args
Then I saved this as apt-proxy.sh, set it to +x (chmod) and everything is working fine when I am in the directory where this file is placed.
My question is : how to add this apt-proxy to PATH so I can actually call it as if it where the real apt-get ? [from anywhere]
Looking for command line only solutions, if you know how to do by GUI its nice, but not what I am looking for.
Try this:
Save the script as apt-proxy (without the .sh extension) in some directory, like ~/bin.
Add ~/bin to your PATH, typing export PATH=$PATH:~/bin
If you need it permanently, add that last line in your ~/.bashrc. If you're using zsh, then add it to ~/.zshrc instead.
Then you can just run apt-proxy with your arguments and it will run anywhere.
Note that if you export the PATH variable in a specific window it won't update in other bash instances.
You want to define that directory to the path variable, not the actual binary e.g.
PATH=$MYDIR:$PATH
where MYDIR is defined as the directory containing your binary e.g.
PATH=/Users/username/bin:$PATH
You should put this in your startup script e.g. .bashrc such that it runs each time a shell process is invoked.
Note that order is important, and the PATH is evaluated such that if a script matching your name is found in an earlier entry in the path variable, then that's the one you'll execute. So you could name your script as apt-get and put it earlier in the path. I wouldn't do that since it's confusing. You may want to investigate shell aliases instead.
I note also that you say it works fine from your current directory. If by that you mean you have the current directory in your path (.) then that's a potential security risk. Someone could put some trojan variant of a common utility (e.g. ls) in a directory, then get you to cd to that directory and run it inadvertently.
As a final step, after following the solution form proposed by #jlhonora (https://stackoverflow.com/a/20054809/6311511), change the permissions of the files in the folder "~/bin". You can use this:
chmod -R 755 ~/bin
make an alias to the executable into the ~/.bash_profile file and then use it from anywhere or you can source the directory containing the executables you need run from anywhere and that will do the trick for you.
adding to #jlhonora
your changes in ~./bashrc or ~./zshrc won't reflect until you do
source ~./zshrc or source ./bashrc , or restart your pc

print a message to the terminal when the user changes to a specific directory?

Is it possible, in unix, to make it so that a system message appears once a user has changed (cd) to a particular directory?
I know about motd, but I'm wondering if there is something similar to that for navigating in the shell. For instance, if I typed
cd /etc/apache2/
a message could be printed to the screen...something like:
"The latest configuration modified in this directory was..."
"Please be careful modifying ... and ..."
something that all users could potentially see?
You could create a script file in each folder that you want to have execute when entering the folder. Then you can use the environment variable PROMPT_COMMAND to check for it and execute. For example:
export PROMPT_COMMAND='test -x ./.prompt_command && ./.prompt_command'
This will execute a script called .prompt_command in the current folder only if it exists and has its executable bit set.

simple shell script to copy files and folders and also execute a command

I haven't written any Shell scripts before, but i have to write a simple shell script to do the following;
I will keep all the required files in a single folder and bundle it with this shell script as a tar file; so when the user runs the shell script, it needs to copy the respective files to the respective destinations.
The execution of copy as follows:
copy the plugin.so file to /usrlib/mozilla/plugins/
copy the .so library files to /usr/local/lib/
copy some header files directories(folders) to /usr/local/include/
and finally, need to do ldconfig.
Basically, you can add in a script any command you are able to type inside the terminal itself. Then, you have two options for executing it:
Execute it from the terminal with sh your_script.sh. You don't even need to give execute permission to it with this solution.
Give it the execute permission and run it with ./your_script.sh.
For the second solution, you have to start the file with what is called a shebang. So your script will look like:
#!/bin/sh
cp path/to/source path/to/destination
cp path/to/source path/to/destination
cp path/to/source path/to/destination
ldconfig
echo "Done!"
Nothing else. Just write the commands one after the other.
The first line is the so-called shebang and tells the shell which interpreter to use for the script.
Note: the extension for shell scripts is usually .sh, but you can actually name your file however you prefer. The extension has no meaning at all.
Good scripting!

Resources