LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-05-2012, 12:00 PM   #1
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Rep: Reputation: Disabled
BASH help


Hi All,
I am newbie in shell scripting, So need your help for this small script.
Code:
$ cat dir_check 
#!/bin/bash
dir="mydir"
###Check whether directory is exist or not###
if [ -d $dir ]; then
echo "The $dir directory is exist, so changing directory to $dir directory"
cd $dir
else
###Create directory and change directory##
echo "The $dir directory does not exist, so creating $dir and changing to $dir directory"
mkdir $dir && cd $dir
fi
1) It is not changing to mydir directory when the directory is exist.
2) just creating the directory but unable to cd while directory does not exist.
Code:
$ pwd
/tmp/test
$ ls
dir_check  mydir
$ rm -rf mydir
$ ls
dir_check
$ bash dir_check 
The mydir directory does not exist, so creating mydir and changing to mydir directory
$ pwd
/tmp/test
$ ls
dir_check  mydir
$ bash dir_check 
The mydir directory is exist, so changing directory to mydir directory
$ pwd
/tmp/test
Thanks in advance for your kind help
 
Old 08-05-2012, 12:17 PM   #2
KinnowGrower
Member
 
Registered: May 2008
Location: Toronto
Distribution: Centos && Debian
Posts: 347

Rep: Reputation: 34
Code:
if [ -d $dir ]; then
Semicolon ";" in the if statement is the culprit. Remove it and its fine.
 
Old 08-05-2012, 12:47 PM   #3
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thanks KinnowGrower,
I removed ";"
Code:
if [ -d $dir ] then
And getting the following error
Code:
$ bash -n dir_check 
dir_check: line 7: syntax error near unexpected token `else'
dir_check: line 7: `else
Then moved "then" to next line.
Code:
if [ -d $dir ]
then
$ bash -n dir_check
$
But still unable to change directory!
Code:
$ pwd
/tmp/test
$ ls
dir_check  mydir
$ bash dir_check 
The mydir directory is exist, so changing directory to mydir directory
$ pwd
/tmp/test
 
Old 08-05-2012, 12:50 PM   #4
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
Quote:
Originally Posted by KinnowGrower View Post
Code:
if [ -d $dir ]; then
Semicolon ";" in the if statement is the culprit. Remove it and its fine.
No, the semicolon is fine if you put the "then" keyword on the same line of if. If you remove it you have to put "then" on the next line.
The problem is that the cd command gets executed in a subprocess of the shell, so that subprocess changes directory but not the parent shell (i.e. the one you used to launch the script).
Try running the script as
Code:
. dir_check
or
Code:
source dir_check
The source (or .) directive forces the shell to execute the commands inside the script in the current process, without forking a new shell.
 
1 members found this post helpful.
Old 08-05-2012, 01:28 PM   #5
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Thanks 414N,
Quote:
. dir_check
or
Quote:
source dir_check
The above both are working fine as what I had expected.
But how should be the crontab entry for this if we add some backup(rsync/tar) commands in the middle?
should this be like below?
Code:
01 00 * * * /path/of/script/file >/dev/null 2>&1
Because since below are not working, I can't guess that above crontab entry works for changing directory.
Code:
chmod +x /path/of/script/file
./path/of/script/file   --->Unable to change directory!
So could you please post also about crontab entry which can change the directory.

Last edited by niharikaananth; 08-05-2012 at 01:30 PM.
 
Old 08-05-2012, 02:19 PM   #6
414N
Member
 
Registered: Sep 2011
Location: Italy
Distribution: Slackware
Posts: 635

Rep: Reputation: 186Reputation: 186
If you need to perform some rsync/tar work inside a specific directory then just cd to the directory and run the backup commands inside the script. You don't need to care about "sourcing" (as in using the source keyword) the script to alter the current directory of the parent shell process.
To explain better, suppose you've got this simple script (foobar.sh) which executes a tar command inside the /foo/bar directory:
Code:
#!/bin/sh

cd /foo/bar
tar cjf foobar.tar.bz2 *
When you launch the script on the command line, the shell (read: parent process) creates a new shell process (read: child process) containing a copy of its environment (including current directory information) and the script gets executed inside this subprocess. Then:
  • the parent process goes to sleep until the child terminates;
  • all changes performed by the script to the shell environment (such as a cd command) are indeed applied only to the child shell process and will not outlive it;
  • the current directory is then changed to /foo/bar and the tar command is performed there;
  • when the child process terminates (after the tar line) it is destroyed and control returns to the parent shell process which awakes from its sleep
You can test this behavior adding simple echo inside your scripts before and after the "cd"s command:
Code:
echo "Current dir is $(pwd)"
cd /some/dir/somewhere/in/a/distant/galaxy/long/long/time/ago
echo "Current dir is $(pwd)"
You should see that the "cd" command is indeed performed, but a pwd command executed after the script will show no change in current directory.

Last edited by 414N; 08-05-2012 at 02:21 PM.
 
1 members found this post helpful.
Old 08-07-2012, 05:11 AM   #7
niharikaananth
Member
 
Registered: Aug 2011
Posts: 58

Original Poster
Rep: Reputation: Disabled
Hi 414N,
Sorry for the late reply. It is working fine using
Code:
$. dir_check
$source dir_check
$bash dir_check
$./dir_check
and
also through crontab
Code:
#!/bin/bash
today=$(date '+%Y-%m-%d')
dir="/data/my_scripts/mydir"
echo $(date) > /tmp/date
###Check whether directory is exist or not###
if [ -d $dir ]; then
echo "The $dir directory is exist, so changing directory to $dir directory"
cd $dir
echo "Current dir is $(pwd)"
sleep 5
tar -cjf tmp.$today.tar.bz2 /tmp
else
###Create directory and change directory##
echo "The $dir directory does not exist, so creating $dir and changing to $dir directory"
mkdir $dir && cd $dir
echo "Current dir is $(pwd)"
sleep 5
tar -cjf tmp.$today.tar.bz2 /tmp
fi
Thanks and Regards

Last edited by niharikaananth; 08-07-2012 at 06:01 AM.
 
Old 08-07-2012, 08:28 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
'sleep 5' is redundant
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Share And Discover Cool Bash Tricks With Bash One-Liners LXer Syndicated Linux News 0 01-30-2012 10:50 AM
Bash problem : -bash: [: /bin/bash: unary operator expected J.A.X Linux - Software 1 09-22-2011 06:52 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 12:10 PM
BASH -copy stdin to stdout (replace cat) (bash browser) gnashley Programming 4 07-21-2008 02:14 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 07:42 PM


All times are GMT -5. The time now is 07:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration