Linux cannot change directory

could not change directory to «/home/corey/scripts»: Permission denied

. the contents of setup_dev_db.sql are executed without any issues, but the error is annoying. Can I get rid of it?

@MarkPlotnick is on the right track. There are too many things going on with your script to easily identify WHICH is causing the problem. However, the quick check would be to review permissions for user postgres on the folder /home/corey/scripts

I suspect it’s simply because the OP ran the script from that directory, which the postgres user can’t read, and the error would go away if the script was run from, say, /tmp .

4 Answers 4

Postgres wants to create a $HOME/.psql_history file, where it will store all your queries and commands from the psql client. It may well want to do something else at $HOME , but I don’t see any evidence in the form of other hidden files. And it won’t actually create the history file unless you’re using psql interactively, which you’re not.

I had this exact same problem and found this question, but the accepted answer wasn’t acceptable to me — I shouldn’t have to grant postgres permission to leave a trail of my queries in whatever directory I happen to be in when I run a script!

@Corey, the solution you mentioned in your comment ( cd /tmp before calling sudo. ) is probably the best. psql won’t create this file in /tmp (I’m sure that’s deliberate, because it could allow unprivileged users to read the file).

There are two other solutions I can think of :

    Run psql in a login shell by adding -i to your command

$ psql dev_db -hlocalhost corey_dev -W < setup_dev_db.sql 

If this is a problem because you leave postgres user creation to your setup_dev_db.sql script, and you don't have any users yet, just add a createuser command in your script first, something like this:

$ sudo -u postgres createuser corey_dev -P 
$ sudo -u postgres createdb dev_db "Dev database" 

NOTE: When using the psql client interactively (which you're not, here), if you see a message like could not change directory to "/home/corey/scripts": Permission denied message ****, psql is going to write to /var/lib/postgres/.psql_history (or wherever its $HOME is)! If you've ever seen that warning when using interactive psql , go look--you'll probably find a hidden history file.

Читайте также:  Vmware converter and linux

Источник

Cannot change directory into home directory, although permissions are set correct

I guess I need some help from an advanced Unix user. On a Ubuntu System I did something stupid. I typed sudo chmod 774 .* or the like, in a subfolder of my home folder and then I suddenly could not change the directory to my home directory anymore. I'm not quite sure about the exact numbers in the command, but I am sure about the .* . Now, after a reboot, I can't even log into the GUI anymore and when I Ctrl + Alt + F1 into a console and log in, I get the message No directory, logging in with HOME=/ . When I then try to cd into /home/myusername , I get the error: -bash: cd: /home/myusername: Permission denied . Also, ls can't access the folder. I checked the permissions of /home/myusername using sudo, and they are all correctly set to: drwxr-xr-x 25 myusername myusername 4096 Aug 26 17:30 myusername A testuser, that I created, has the same promblems. It cannot log into the GUI, either. Only a guest session can do it (I guess that is because the guest home folder is created inside /tmp ). What do I need to do, to restore my permissions/get access to my home folder again. Any suggestions? Cheers, Sebastian

What are the permissions on /home ? Maybe there is a x permission missing for a directory? ls -ld /home

@VolkerSiegel There was indeed an x permission missing for world in /home . Seems like this solved the problem! I will report, once I have confirmed that all is functioning.

Источник

Changing directory does not work in shell script

The above does not change the directory. Shell is running in its own context so it can not provide linux terminal with changed drive (into driver) but if I give cd /driver ls It gives the proper output of ls in driver directory again comes out of driver directory Can anybody help me to get terminal with actually changed path (into driver) .

Читайте также:  Network information system in linux

5 Answers 5

If you run your script with

you are opening a sub-shell where the commands of the script are executed. Changing directory in that sub-shell has no effect on the working directory of the shell that you call your script from. If instead you type

you should get the desired result.

do "source script_name". It will change the directory

You could start a shell or terminal in the script, after you set the directory.

file: driver, remember to set x permission

Running driver will produce another shell prompt.

The current directory is now driver.

Typing "exit" or control-D will go back to the old shell, with its previous directory.

Your script is an executable file:

Just to verify the above problem's root cause you can do the following

read a echo "You entered : $a" 

Save it (say script.sh) and change the permission accordingly if needed.

In same tab run 'ps' command and note the shell pid (say p1).

Now run the script (i.e. ./script.sh)

Script will ask for the input but don't provide the input. Now in another tab run somthing like 'ps -ef | grep pts'. Here you will find that there are two shell process. You have one more shell whose ppid is equal to the pid of previous shell i.e. p1.

So basically each shell script invocation creates a new process and hence a new context.

Источник

Cannot change directory from within shell script [duplicate]

I want to write a shell script where i change directory and then execute a command inside that directory. How come i cannot change directory? It does not work whenever i do:

#!/bin/bash cd /something/something 

You are perfectly changing the directory. However, just within the scope of the script, so when it finishes you are back into the reality of the session you were working on. You may want to use source file instead, so that the command gets executed in the current shell.

Читайте также:  Linux logout all sessions

1 Answer 1

When you run a shell script a new shell is created. So thus when the shell script exits the shell the script is running in is destroyed and you thus return to your current shell. Which is probably a good thing since in some shell scripts you are making a ton of variables and stuff of that nature that you don't want lurking around after the script is done executing.

Due to the shell script shell being blown away and you being returned to you original shell the change of directory will not be reflected in the interactive shell you are using.

There is a command called source though which executes the shell script in the current shell and thus would cd in your current shell and make any other changes done in the script until you exit your current shell.

If however in the script you are using there is no cd at all happening when the new shell is made, this can be due to the file permissions on a directory. If a directory has a chmod -x no execute permission set, you can not cd into that directory as you need execute permissions on a directory to enter that directory.

Источник

Оцените статью
Adblock
detector