LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Elementary Script Trouble (https://www.linuxquestions.org/questions/linux-newbie-8/elementary-script-trouble-832958/)

mwesolow 09-18-2010 12:19 AM

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

Dark_Helmet 09-18-2010 12:42 AM

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.

14moose 09-18-2010 12:44 AM

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!

mwesolow 09-18-2010 11:50 AM

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;


Dark_Helmet 09-18-2010 03:26 PM

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


mwesolow 09-19-2010 04:17 PM

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).

grail 09-19-2010 07:29 PM

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

chrism01 09-20-2010 12:55 AM

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


All times are GMT -5. The time now is 09:20 PM.