Problem creating script . sh to enter a directory

Asked

Viewed 1,757 times

1

I am creating a script. sh that the first step is to enter a directory.

1.

#!/bin/bash
cd /home/salariosbrasil/consulta-ninja
#git pull
pwd

this directory exists and yet the cd doesn’t work..

but if I test the mkdir -p with the same directory name

#!/bin/bash
mkdir -p /home/salariosbrasil/consulta-ninja
cd /home/salariosbrasil/consulta-ninja
#git pull
pwd

it creates another directory with the same name. only with "?" at the end.

inserir a descrição da imagem aqui

My question is:

  • How to do to the cd work?
  • Because it creates another directory with the same name with "?" at the end?
  • Which message appears when you run ? Direct on command line works ?

  • It looks like your file script.sh contain strange characters... (makes od .c script and see if any strange character appears (example \r) after the directory name ; by the way: what gives ls -Q?

3 answers

3

The command cd inside the bash (.sh) runs under a subshell.

The action is successfully executed however, the main shell has no effect because it was executed in a different context.

A simple trick is to invoke the . sh

We usually do that

> ./file.sh

Well, just add the command dot with a space at the beginning:

> . ./file.sh

Alternatively, you can change the dot for source

> source ./file.sh

The command . (dot/period) is a synonym for the command source. This command loads routines/functions from a file into the current shell.

Runs in a new shell:
./file.sh

Returns execution in current shell:
. file.sh

  • Daniel, what is the effect of adding the point at the beginning?

  • I edited at the end.

2


The problem was that I created the script in Windows and sent it to the linux machine to run the .sh :D :D

I created the script on vi and worked perfectly.

"Unix uses Different line feeds and Carriage Returns so can’t read the file you created on Windows. Hence it is Seeing M as an illegal Character"

0

Maybe it could be a security level issue... Have you already taken a look if the user of estra running the script without permission to read such directory ?

I use this routine in the company:

#!/bin/bash

make-dir(){
  sudo mkdir /solinftec
  sudo mkdir /solinftec/bin
  sudo mkdir /solinftec/log
  sudo mkdir /solinftec/sgpa-api
}

make-dir

Also check how this flag [-p] works in your distro, use Fedora, and with me it does not happen this.

mkdir --help

Usage: mkdir [OPTION]... DIRECTORY...
Create the DIRECTORY(ies), if they do not already exist.

Mandatory arguments to long options are mandatory for short options too.
  -m, --mode=MODE   set file mode (as in chmod), not a=rwx - umask
  -p, --parents     no error if existing, make parent directories as needed
  -v, --verbose     print a message for each created directory
  -Z                   set SELinux security context of each created directory
                         to the default type
      --context[=CTX]  like -Z, or if CTX is specified then set the SELinux
                         or SMACK security context to CTX
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/mkdir>
or available locally via: info '(coreutils) mkdir invocation'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.