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-18-2010, 01:19 AM   #1
mwesolow
LQ Newbie
 
Registered: Sep 2010
Location: Waterloo, ON
Distribution: GNU/Linux
Posts: 7

Rep: Reputation: 0
Elementary Script Trouble


When testing my script for a question, I find that when I execute the
expanded commands, I get no error message, but when I source the file with
the command, I get 'No such file or directory'. Here is what I mean:
Code:
    mwesolow@linux016:~/cs246/1$ ./q1c
    ./q1c: line 2: cp /u5/mwesolowski/cs246/1/.snapshot/hourly.3/q1a /u5/mwesolowski/cs246/1/q1a: No such file or directory
    ./q1c: line 2: cp /u5/mwesolowski/cs246/1/.snapshot/hourly.3/q1b /u5/mwesolowski/cs246/1/q1b: No such file or directory
    ./q1c: line 2: cp /u5/mwesolowski/cs246/1/.snapshot/hourly.3/q1c /u5/mwesolowski/cs246/1/q1c: No such file or directory
    ./q1c: line 2: cp /u5/mwesolowski/cs246/1/.snapshot/hourly.3/q1d /u5/mwesolowski/cs246/1/q1d: No such file or directory
    ./q1c: line 2: cp /u5/mwesolowski/cs246/1/.snapshot/hourly.3/q1e /u5/mwesolowski/cs246/1/q1e: No such file or directory
    mwesolow@linux016:~/cs246/1$ cp /u5/mwesolowski/cs246/1/.snapshot/hourly.3/q1a /u5/mwesolowski/cs246/1/q1a
    mwesolow@linux016:~/cs246/1$
Could anyone point me to what I am doing wrong?

Here is my actual 'script':
Code:
#!/bin/bash
set -x
for x in `ls -1 ./.snapshot/hourly.3/`; do if [ ! -d "./.snapshot/hourly.3/$x" ]; then "cp `pwd`/.snapshot/hourly.3/$x `pwd`/$x"; fi; done;

Thanks

Last edited by mwesolow; 09-18-2010 at 01:20 AM. Reason: Addition of Code
 
Old 09-18-2010, 01:42 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Type this exactly at a shell prompt and look at the results
Code:
"/bin/echo hi there"
If you do not get a "No such file or directory" error after trying that, then you did not type it exactly as it's written.

Once you realize why that causes an error, then review your script for something similar.

I'm avoiding a direct answer for a reason. This is obviously a programming assignment. And LQ policy is not to give answers to those kinds of questions. However, nudging and pointing-in-the-right-direction tends to be ok. So, the above is all I can say without answering it directly.
 
1 members found this post helpful.
Old 09-18-2010, 01:44 AM   #3
14moose
Member
 
Registered: May 2010
Posts: 83

Rep: Reputation: Disabled
Hi -

I'm a bit confused about a number of things you're trying to do. But this snippet might help:
Code:
#!/bin/bash
SRC=./.snapshot/hourly.3
DST=./.snapshot/backup
echo SRC=$SRC DST=$DST
for f in `ls -1 $SRC`; do 
  if [ ! -d "$SRC/$f" ]; then
    echo "cp $SRC/$f $DST" 
    cp $SRC/$f $DST
  fi
done
PS:
I noticed Dark Helmet and I posted at the same time. He's correct - I encourage you to follow his advice and figure out why.

But that's not the only problem with your script. Please post back with what you find, and your final (working) version!

Last edited by 14moose; 09-18-2010 at 01:48 AM.
 
1 members found this post helpful.
Old 09-18-2010, 12:50 PM   #4
mwesolow
LQ Newbie
 
Registered: Sep 2010
Location: Waterloo, ON
Distribution: GNU/Linux
Posts: 7

Original Poster
Rep: Reputation: 0
Thank you for your kind replies!

Yes, this is a programming assignment, so thank you for considering that. Here is the final working script:

Code:
#!/bin/bash
set -x
for x in `ls -1 ./.snapshot/hourly.3/`; 
  do 
  if [ ! -d "./.snapshot/hourly.3/$x" ]; 
    then cp "`pwd`/.snapshot/hourly.3/$x" "`pwd`/$x"; 
  fi; 
done;
 
Old 09-18-2010, 04:26 PM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
I'm glad we could help.

You might continue refining this script in the course, or the instructor might throw different types of input to test how robust your script is. So, I'd also like to give you a heads-up on a potential hiccup. This might be one of the things 14moose was getting at also.

It might be a good idea for you to read up on the special variable IFS (man bash). Your use of double quotes around filenames with the cp command tells me that you're concerned the filenames might, for instance, have spaces in them. If that is the case, your for loop will not behave as you expect.

As an example, try the following:
Code:
$ mkdir testing
$ cd testing
$ touch my_test_file1.txt
$ touch "my test file2.txt"
$ for x in `ls -1`; do echo $x; done
 
Old 09-19-2010, 05:17 PM   #6
mwesolow
LQ Newbie
 
Registered: Sep 2010
Location: Waterloo, ON
Distribution: GNU/Linux
Posts: 7

Original Poster
Rep: Reputation: 0
I see the problem now. I suppose I could set IFS to some non-printable character to reduce all likelihood of a problem, and then set it back to <space><tab><newline> at the end. But if there is a way to get around this without changing environment variables, that would be preferable.

My bash scripts will be tested automatically, so I don't know if we will even have rights to change IFS (I will ask, but this seems a bit much for question 1c on the first assignment).
 
Old 09-19-2010, 08:29 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,692

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Try the same example provided by Dark Helmet but try changing the for loop to use globbing:
Code:
for x in *; do echo "$x"; done
I would also suggest moving away from ls as an input source as it can have undesirable affects as listed here
 
1 members found this post helpful.
Old 09-20-2010, 01:55 AM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
IFS is just a shell var (albeit automatically setup for you). Any shell prog can change its own copy, but of course, you cannot export it back up the process tree
Typically you'd use it something like this
http://www.tldp.org/LDP/abs/html/int...bles.html#IFSH
 
1 members found this post helpful.
  


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: What is Nautilus Elementary And How to Install Nautilus Elementary in Ubuntu Lu LXer Syndicated Linux News 0 05-21-2010 02:10 AM
elementary help !! matlin Linux - Newbie 2 02-21-2008 09:56 AM
Elementary security issues: hopefully useful to all eoinrua Linux - Newbie 4 11-04-2007 12:05 PM
[SOLVED] Elementary Bash Scripting Question Eternal_Newbie Slackware 2 08-25-2005 07:40 PM
Elementary IP question Rotwang Linux - Networking 14 03-11-2004 05:44 PM


All times are GMT -5. The time now is 04:56 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration