Cd then mkdir from script

Importing images from a camera (or two).
I sort by date (1901 this month).

Currently (failing)

if [[ -d $pana ]]
then
 echo "Found Panasonic G9X"
      #echo "List files on camera"
      #ls ${pana}/*
       . chxdir.sh ${photos}/$mn

I want to change directory to a fixed base ($photos)/$mn obtained from current year month.

How to get this to work please?
Must I call the script with . script or some such?
Frustrating!

Can you explain:
. chxdir.sh ${photos}/$mn ?
And tell us what shell you are using ( looks like bash...)

1 Like

An attempt to change directory, with another script? Which failed,
And yes, using bash on fedora (though happy with zsh)

Not knowing the contents of ${photos}, $mn, or chxdir.sh, we cannot begin to guess. Try set -x to run with debug trace.

As a start, verify that all your variables are what you think they are with echo . chxdir.sh ${photos}/$mn And if you edited any of your scripts in Microsoft Notepad, that will have filled them with invisible carriage return characters which you must delete with tr -d '\r' < inputfile > outputfile for it to work. outputfile must be a different name than inputfile.

Otherwise, not knowing the contents of ${photos}, $mn, or chxdir.sh, I cannot begin to guess. My crystal ball is out of order.

Stripped test.

current directory = /a
target= /b
mn=`date  +%y%m`

I want to

cd to $tgt
cp /a/*.jpg to ./${mn}

I can't get my head round the shell script working directory vs current working directory
Hence the failed chxdir script

a synonym for a point . is "source".
This means not the execution of the script but the insertion of the contents of this file into the current location.
This command has no second parameter

1 Like

Hi,

Your additional information in post #3 is insufficient, where does $tgt come from?

A good place to start would be to post the output from chxdir.sh and the script that it is called from, this will at least allow us to see what the scripts do.

Regards

Gull04

1 Like

I am late into this thread, but why not walk before you can run...
As we have no idea what is in chxdir.sh nor the complete script that calls it we have to make wild guesses...
This is one way to get to change a directory on the fly from one script into another...
This is the script to be sourced, I have used the statement "source" instead of its shortcut "."...
The only proviso is you need to intitally know where both scripts are...

#!/bin/sh
# chxdir.sh
NEWDIR="$HOME"/Temp
echo "Now inside chxdir.sh..."
echo "Switching to new directory, $NEWDIR in another script..."
# Leave NEWDIR and do other stuff in here...

This is the master script...

#!/bin/sh
OLDDIR=$( pwd )
NEWDIR=$( pwd )
echo "Old directory, $OLDDIR, new directory $NEWDIR..."
echo "This is a test script..."
echo "Launching chxdir.sh..."
source "$HOME"/Desktop/Code/Shell/chxdir.sh
echo "Now back inside chdirtest.sh..."
cd "$NEWDIR"
echo "Now inside $NEWDIR, $OLDDIR unchanged..."
ls
echo "Now exiting back into the default shell and its current default directory..."

Results of the experiment using OSX 10.14.1, default bash terminal...

Last login: Wed Jan  9 12:22:30 on ttys000
AMIGA:amiga~> cd Desktop/Code/Shell
AMIGA:amiga~/Desktop/Code/Shell> chmod 755 chxdir.sh
AMIGA:amiga~/Desktop/Code/Shell> chmod 755 chdirtest.sh
AMIGA:amiga~/Desktop/Code/Shell> ./chdirtest.sh
Old directory, /Users/amiga/Desktop/Code/Shell, new directory /Users/amiga/Desktop/Code/Shell...
This is a test script...
Launching chxdir.sh...
Now inside chxdir.sh...
Switching to new directory, /Users/amiga/Temp in another script...
Now back inside chdirtest.sh...
Now inside /Users/amiga/Temp, /Users/amiga/Desktop/Code/Shell unchanged...
0000000000.BIN		dcdata.raw		squarewave.raw
1KHz-Test.sh		pulse1.wav		sweep.raw
Arduino_9600.pde	pulse2.wav		sweep.wav
FFT_WAV.py		pulsetest.sh		sweeper.raw
Untitled.m4a		sample.raw		symmetricalwave.raw
VERT_BAT.BAT		signed16bit.txt		symmetricalwave.wav
VERT_DSP.sh		sinewave.raw		waveform.raw
VERT_SOX.sh		sinewave.wav		waveform.wav
Now exiting back into the default shell and its current default directory...
AMIGA:amiga~/Desktop/Code/Shell> _

Note I have initiated "NEWDIR" on both, not necessary but useful to keep track of used variables...
Now you know that it is possible transfer 'paths' give us your two scripts so that we can peruse them properly, unless you have some ultra-secret code you don't want making public.

See my earlier simplified script. It's where I want to cd to, then copy files into.

tgt=/b

If i put that all together correctly you have two problems:

1) obtain the directory name from the date in the form you want.

2) change into that directory or create it if it is not there.

Assuming that this is correctly stated, here are your solutions:

1) use the date command. It wil output the current date (and time) but you can give it a "format string" which will determine in which form you want to output to be formatted. Part of this "formatting" is also the addition or removal of certain parts of the date.

To get the current date and time:

$ date
Wed Jan  9 20:36:44 CET 2019

Now, let us look at the man page of date (this should always be your first reference when trying to figure out what a command does and how it does it):

$ man date

DATE(1)                          User Commands                         DATE(1)

NAME
       date - print or set the system date and time

SYNOPSIS
       date [OPTION]... [+FORMAT]
       date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]

DESCRIPTION
       Display the current time in the given FORMAT, or set the system date.

OK, so we have date +format where we still have to determine what "format" should be. So, reading further in the man page, we find (among many other formats):

       FORMAT controls the output.  Interpreted sequences are:
[...]
       %m     month (01..12)
[...]
       %y     last two digits of year (00..99)

So let us have a try:

$ date '+%y%m'
1901

Problem 1 solved.

For the second problem: you do not need to test it! mkdir knows the -p option, which will create a directory, if it is not there - complete with the whole path leading up to it. Instead of:

$ mkdir /some
$ cd /some
$ mkdir where
$ cd where
$ mkdir subdir

you can as well write

$ mkdir -p /some/where/subdir

Which will create the directory /some if it doesn't exist (if it does exist, nothing happens), the in it this directory create /some/where if it does not exist (again, if it does nothing will happen), and so on.

So, your script could contain something like:

myroot="/some/where"
curdir="$(date '+%y%m')"      # this executes "date '+%y%m'" and assings its output to variable curdir

mkdir -p "$myroot/$curdir"      # make sure the directory exists.
[...] rest of your code here, using "$myroot/$curdir" as path to your files

As a general rule: never use relative pathes in scripts, always only absolute pathes. This way your scripts will always do the same to the same files and will not produce different results just because you called them from a different directory. Also, never use cd in a script. First, you will not need it if using absolute pathes and second, you shouldn't have to change the environment for a script to use. If you want to copy files to a directory you do NOT do:

cd /some/where
cp /what/ever/* .

But instead you do:

cp /what/ever/* /some/where

I hope this helps.

bakunin