-1

The following commands work on my terminal but not in my shell script. I later found out that my terminal was /bin/tcsh. Can somebody tell me what changes I need to do for /bin/sh. Here are the commands I need to change:

cp source_dir/*/dir1/*.xml destination_dir/
Error in sh-> cp: cannot stat `source_dir/*/dir1/*.xml': No such file or directory

sed -i "s+${initial_name}+${final_name}+" $file_name
This one does not complain but does not work as well.

I am adding an example for testing. The code tends to rename the names of xml files and also the contents of xml files. For example-

  • The file name crr.ya.na.aa.xml should be changed to aa.xml
  • The same name inside crr.ya.na.aa.xml should also be changed from crr.ya.na.aa to aa

Here is the code:

#!/bin/sh

# Create dir structure for testing
rm -rf audience
mkdir audience
mkdir audience/dir1 audience/dir2 audience/dir3
mkdir audience/dir1/ipxact audience/dir2/ipxact audience/dir3/ipxact
touch audience/dir1/ipxact/crr.ya.na.aa.xml
echo "<spirit:name>crr.ya.na.aa</spirit:name>" >   audience/dir1/ipxact/crr.ya.na.aa.xml
touch audience/dir2/ipxact/crr.ya.na.bb.xml
echo "<spirit:name>crr.ya.na.bb</spirit:name>" >   audience/dir2/ipxact/crr.ya.na.bb.xml
touch audience/dir3/ipxact/crr.ya.na.cc.xml
echo "<spirit:name>crr.ya.na.cc</spirit:name>" >   audience/dir3/ipxact/crr.ya.na.cc.xml

# Create a dir for ipxact_drop files if it does not exist
mkdir -p ipxact_drop
rm -rf ipxact_drop/*
cp audience/*/ipxact/*.xml ipxact_drop/

ls ipxact_drop/ > ipxact_drop_files.log

cat ipxact_drop_files.log | \
awk '{ split($0,a,"."); print a[length(a)-1] "." a[length(a)] }' ipxact_drop_files.log > file_names.log

cat ipxact_drop_files.log | \
awk '{ split($0,a,"."); print "mv ipxact_drop/" $0 " ipxact_drop/" a[length(a)-1] "." a[length(a)] }' ipxact_drop_files.log > command.log

chmod +x command.log
./command.log

while read line
  do
    echo ipxact_drop/$line
    initial_name=`grep -m 1 crr ipxact_drop/$line | sed -e 's/<spirit:name>//' | sed -e 's/<\/spirit:name>//' `
    final_name="${line%.*}"
    echo $initial_name
    echo $final_name
    sed -i "s+${initial_name}+${final_name}+" ipxact_drop/$line  
done < file_names.log

echo " ***** SCRIPT RUN FINISHED *****"

Only the sed command at the end is not working

valar_m
  • 23
  • 7
  • What does the very first line of the script look like (the so-called ["shebang"](https://en.wikipedia.org/wiki/Shebang_(Unix)) line)? – Some programmer dude Jan 17 '18 at 11:38
  • Here is the first line: #!/bin/sh -f – valar_m Jan 17 '18 at 11:39
  • And if you change the `sh` to `tcsh`? How does it work (or not work) then? – Some programmer dude Jan 17 '18 at 11:42
  • Yes, I tried this easy hack. But then I get syntax errors with shell commands like : 'while read line'. Best would be to understand what I missed here – valar_m Jan 17 '18 at 11:43
  • 1
    By the way, the first error have nothing to do with the shell used, but about working directories. The working directory of the script might not be what you expect it to be, and so it can't find the files or directories you list. As the error message says. This is also the reason you get syntax errors when changing to use `tcsh` in the script, because at least the second line you show uses `sh` specific functionality that I donät believe `tcsh` have. – Some programmer dude Jan 17 '18 at 11:43
  • I run the command and the script from the same terminal and same directory – valar_m Jan 17 '18 at 11:45
  • And the script doesn't change directory? Without knowing the full script, what it does, what it's supposed to do, then we really can't do anything more than ***guess***. Now please take some time to read [the help pages](http://stackoverflow.com/help), [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 17 '18 at 11:46
  • The "-f" on your shebang line means "disable filename generation" (at least, at http://heirloom.sourceforge.net/sh/sh.1.html ), so that's probably the cause of your first error – racraman Jan 17 '18 at 11:48
  • By the way, is there any particular reason for using /bin/sh ? For writing new scripts from scratch, I'd be going for later shells (eg bash, etc). – racraman Jan 17 '18 at 11:57
  • Yes, -f is the cause of first error. – valar_m Jan 17 '18 at 11:58
  • Thank you for the recommendation, I will try to use bash in future. I made a small experiment and changed shebang to #!/bin/bash. I don't get any error but the sed statement doesn't work here as well. – valar_m Jan 17 '18 at 12:03
  • @racraman: Nowadays `/bin/sh` is just an alias to `/bin/bash`. – mouviciel Jan 17 '18 at 12:05
  • @mouviviel Thanks for that - ouch, showing my age !! – racraman Jan 17 '18 at 12:10
  • @valar_m For the sed command, the "-i" is expecting an extension (used for making the backup file) - eg sed -i .back "s/from/to/" $file – racraman Jan 17 '18 at 12:14
  • Dear All, I have added an example for testing – valar_m Jan 17 '18 at 12:57
  • Thanks - but you still have not put an extension in the sed command after the -i . – racraman Jan 17 '18 at 13:00
  • Try sed -i ".back" "s+${initial_name}+${final_name}+" ipxact_drop/$line – racraman Jan 17 '18 at 13:01
  • @ racraman - After adding .back extension, I get the desirable result with the example I gave for testing where each xml file has just 1 line. But as soon I try it with a real xml with lets say 1500 lines, the sed does not work any more. The code is still printing correctly the initial_name and final_name variables. Is there any limitation of sed w.r.t file size? – valar_m Jan 18 '18 at 09:18
  • Some sed implementations have limits on the length of the lines, but not really on number of lines; I'd be very surprised if 1500 lines like your example would cause a problem. What "does not work anymore" - what error are you getting ? I suspect it's data related - What happens if you put the first few lines that "don't work" and put them in your small example above so you can run just them ? – racraman Jan 18 '18 at 11:29
  • By 'sed does not work anymore' I mean that the 'initial_name' is not replaced by the 'final_name'. It stays as it is. I don't get any error. The substitution is simply not done. I tried to test the above example by putting some lines before and after the text present in the example xml files. The code works in that case. I will try to come with a small example that fails :) – valar_m Jan 19 '18 at 07:28
  • I would suggest to strip (a copy of) your code down to the bare minimum, to reproduce the bug with some sample data, you provide, to reproduce the behaviour. Most of the code, afaik, makes no problem, right? – user unknown Feb 02 '18 at 11:01
  • @ user unkown- The code works with the sample xml file I provided. It does not with full blown xml file with 1000 lines for example. – valar_m Feb 05 '18 at 14:20

1 Answers1

0

I was reading some other posts and understood that xml files can have problems with scripts. Here is what that worked for me upto now.

To remove cp error: replace #!/bin/sh -f with #!/bin/sh

To remove sed error for the test input: replace sed -i ...... with sed -i.back ....

valar_m
  • 23
  • 7