Change the directory where the script resides in - bash

I'm trying to change the directory name where the BASH script resides in.
I've built following function inside this script:
function rename_dir(){
echo "Gimme new dir name: "
read new_dir_name
mv ../${PWD##*/} ../$new_dir_name
cd ../$new_dir_name
Basically it does the job.
When I run it manually by calling ./script.sh it changes the directory name but since it's running in a subshell - I need to do:
cd ..
cd New_Dir_name
And that's fine and understandable. No biggie.
When I source the script it naturally does the job but when I rename the directory the script loses the 'X' attribute and I need to chmod it again.
Any other approach to overcome this? Anyone has any other ideas how to get it done properly?

Related

Bash script change directory returns no such file or directory

I'm working a script that reads back the input of a full path to a directory (e.g. ~/test) but when I run the shell script it come back with this:
test-script.sh: line 34: cd: ~/test: No such file or directory
I'm running the test-script.sh file in a different directory, so I was assuming that a simple:
cd ~/test in the script would do it, but I guess not.
I know that this is probably a redundant question, but most of the other issues/examples have been with loops and other cases outside of just doing a simple cd ~/test in a script.
And for reference this is all I'm doing in my script:
echo What directory do you want to change to?
read directory_name
cd $directory_name
Again, new to bash/shell scripting so if there are any other ideas or suggestions, I'm all for it. Thanks!
solution 1)
if you want to use ~ , you need to use eval .
#!/bin/bash
pwd
echo What directory do you want to change to?
read directory_name
eval "cd $directory_name"
pwd
But this solution is weak , because someone can enter some maliciuois command instead of a directory name and this script will execute .
solution 2)
check if the first character of directory_name is a ~ and replace it by the appropriate value , it can the current user home dir or another user home dir .
you can do cd ~ or cd ~usera to change to usera homedir

Moving a file from a directory to home directory using shell script, it works as a standalone command(as in outside the .sh file)

Below is my .sh file
sh summaryByClient.sh $1 - takes around 10 mins to fetch the required data
mv summary.html ~/public_html/chats/ - **this is not happening**
exit 0
I do not understand why mv summary.html ~/public_html/chats/ this is not working inside .sh file, However I am able to mv separately using the same above command.
Could you be running into the issue from this answer with expanding the user's home directory? What happens if you write your script like this:
#!/bin/bash
# Other tasks to retrieve summary.html done here
mv ./summary.html $HOME/public_html/chats/
exit 0
Also, it's always a good idea to check whether the destination directory exists before a mv command. Examples are shown in this answer

How can I run executable in shell script?

I have folder "destination/" and inside the destination folder, it contains executable grade1. My current directory is "destination/"
I'm Trying to run
for i in FILES/*; do ./grade1 $i done
Which it keep says
./grade1 no such file or directory
Weird part is that, when I just copy the code to command line and run it, it works fine. Problem only arises when I do it in shell script
I don't think that grade1 really is in your current working directory, as you claim. I suggest that you verify this also check the permissions of your executable. Extend your code to
echo current directory is $PWD
ls -l grade1
for i in FILES/*; do ./grade1 $i done
This should reveal the source of your problem.
One simple way to fix this problem might be to start using FULL path of grade1 executable file
for i in FILES/*; do {FULL_PATH}/grade1 $i done

Calling a script within a script that was run via nohup

I have a script, which when I run to screen, works perfectly.
The directory structure is as follows:
/home/username/processing/ScriptRunning
/home/username/processing/functions/include_me
In the script, it opens another script, which contains a function by simply doing this:
#!/bin/bash
#This is ScriptRunning script
. functions/include_me
Now when I call the script using the following nohup command:
nohup /home/username/processing/ScriptRunning
this is the output:
/home/username/processing/ScriptRunning: line 3: /home/username/functions/include_me: No such file or directory
It seems to be missing out the processing directory
I've altered the line within the ScriptRunning to have a full path, both hardcoded to /home/username/processing and also having this as a variable created by calling $(pwd), but the error is the same.
Am I really missing something so stupid?
This isn't a nohup issue. You are including a source file using a relative file name. Try:
. $(dirname ${BASH_SOURCE})/functions/include_me
to include a source file located relative to ${BASH_SOURCE}

Cd in shell script not working

First off I am very very new to shell scripting. I am trying to write a script that takes in one parameter and then copies a folder in a different directory naming it using the parameter. This is the current code that I have:
#!/bin/sh
cd /var/www/html/fbplugin/chrome
sudo mkdir temp/$1
sudo cp -rf "/var/www/html/fbplugin/chrome/fbplugin" "/var/www/html/fbplugin/chrome/temp/$1"
When I run this code it says can't cd to /var/www/html/fbplugin/chrome. I'm not sure why it is saying this because I know the directory exists. I have copied the line directly and it works in terminal. If anyone could help me out that would be great.
If it matters in order to run the script I am typing "sh build.sh"
If that directory really exists, then you must have executed that script with a different user (cron, webserver, etc).
Check the rights for that directory.
I don't know why you're getting the error about cd, but it looks like you could just use absolute paths throughout. That would solve the larger problem of the script working correctly.

Resources