LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 09-13-2009, 06:58 PM   #1
Juststartedusinglinux
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Rep: Reputation: 0
Why are mu commands not being recognised in a shell scipt


I have a very simple script which consists of a few lines to move rename a file. It just does not seem to work. It keeps giving me and error No Such files or directory. The contents of the script asr

# << Script to archive log files >>
#!/bin/bash
cd /home/db2inst1/sqllib/db2dump
mv db2diag.log "db2diag.log_`date"

It keep coming back with an error message
No such files or directoryin 3 cd: /home/db2inst1/sqllib/db2dump

Can anyone help.

Please be gentle with the acronyms as I am pretty new to unix/linux.
 
Old 09-13-2009, 07:26 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,241

Rep: Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325Reputation: 2325
Quote:
No such files or directory in 3 cd: /home/db2inst1/sqllib/db2dump
means exactly what it says, /home/db2inst1/sqllib/db2dump does not exist as a dir, or you don't have correct permissions to cd to that dir.
Incidentally, you missed the 2nd backquote (`) in the next line also.
 
Old 09-13-2009, 08:37 PM   #3
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by Juststartedusinglinux View Post
I have a very simple script which consists of a few lines to move rename a file. It just does not seem to work. It keeps giving me and error No Such files or directory. The contents of the script asr

# << Script to archive log files >>
#!/bin/bash
cd /home/db2inst1/sqllib/db2dump
mv db2diag.log "db2diag.log_`date"

It keep coming back with an error message
No such files or directoryin 3 cd: /home/db2inst1/sqllib/db2dump

Can anyone help.

Please be gentle with the acronyms as I am pretty new to unix/linux.
Problem 1:

Quote:
# << Script to archive log files >>
#!/bin/bash
The "shebang" line must be the first line in your script, like this:

Code:
#!/bin/bash
# << Script to archive log files >>
Problem 2:

Code:
cd /home/db2inst1/sqllib/db2dump
mv db2diag.log "db2diag.log_`date"
If Bash reports that a file or a directory doesn't exist, chances are it doesn't exist. Check whether it exists.

Here is a better way to write the script:

Code:
dir="/home/db2inst1/sqllib/db2dump"
target="db2diag.log"

if [ -e $dir ]
then
   cd $dir
   if [ -e $target ]
   then
      mv $target "${target}_`date`"
   else
      echo "file $target in $dir doesn't exist."
   fi
else
   echo "directory $dir doesn't exist."
fi
 
Old 09-14-2009, 04:08 PM   #4
Juststartedusinglinux
LQ Newbie
 
Registered: Sep 2009
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by lutusp View Post
Problem 1:



The "shebang" line must be the first line in your script, like this:

Code:
#!/bin/bash
# << Script to archive log files >>
Problem 2:

Code:
cd /home/db2inst1/sqllib/db2dump
mv db2diag.log "db2diag.log_`date"
If Bash reports that a file or a directory doesn't exist, chances are it doesn't exist. Check whether it exists.

Here is a better way to write the script:

Code:
dir="/home/db2inst1/sqllib/db2dump"
target="db2diag.log"

if [ -e $dir ]
then
   cd $dir
   if [ -e $target ]
   then
      mv $target "${target}_`date`"
   else
      echo "file $target in $dir doesn't exist."
   fi
else
   echo "directory $dir doesn't exist."
fi
Thanks very much for your help folks, It has made my day - the script ran successfully after I took on board your comments and wrote the script in vi.
 
  


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
How to start a new konsole to execute a shell scipt odin147 Programming 5 09-16-2006 03:31 AM
Need a shell scipt to get 2 variables per line and then run a command var 1 is over 5 abefroman Programming 2 02-18-2006 04:46 PM
how to include a shell scipt or call it at startup abhis_mail2002 Fedora 6 12-13-2005 12:30 AM
MySQL commands not recognised by PHP4 Christopher Linux - Software 1 12-09-2004 06:16 PM
'make' & 'make install commands not recognised by shell gilbertt Linux - Software 4 05-04-2004 09:04 AM


All times are GMT -5. The time now is 01:23 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