Click to See Complete Forum and Search --> : shell script not working


HelpPlease
12-10-2003, 11:21 AM
Not sure if the coding itself is wrong...........(copy 3 files into 1 file)
#!/bin/sh^M
chmod +x last.sh^M
if [-f $4] ; then ^M
rm -f $4 ^M
touch $4^M
cat $1 $2 $3 >> $4^M
sh <file> file1 file2 file3 <target file>^M
fi
~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"last.sh" [Incomplete last line] 8 lines, 135 characters

or i'm executing it wrong. i've tried.........
$ ./last.sh
bash: ./last.sh: bad interpreter: Permission denied
[~]
$ sh last.sh
: No such file or directory
last.sh: [-f: not found
[~]
$ ./last.sh together.txt file1.txt file2.txt file3.txt
bash: ./last.sh: bad interpreter: Permission denied
[~]

jim mcnamara
12-10-2003, 11:25 AM
execute chmod +x from the command line - not inside the script

It also looks like you either do not have /bin/sh or do not have permission to execute it.

change#!/bin/sh to #!/bin/bash

andysimmons
12-10-2003, 11:37 AM
It has a bunch of MS-style carriage returns in it. Fix it by typing "dos2unix last.sh" on the command line. In your script, #!/bin/sh should work fine; the bad interpreter error will continue to come up until you nuke those carriage returns.

HelpPlease
12-10-2003, 11:48 AM
Tried to fix it by typing "dos2unix last.sh" on the command line..........not working........
$ dos2unix last.sh
could not open /dev/kbd to get keyboard type US keyboard assumed
could not get keyboard type US keyboard assumed
#!/bin/bash
if [-f $4] ; then
rm -f $4
touch $4
cat $1 $2 $3 >> $4
sh <file> file1 file2 file3 <target file>
fi[~]

what does that mean?

andysimmons
12-10-2003, 12:07 PM
Actually what you have up there still isn't going to work. Your if/then has a couple lines that don't belong there, and the script itself has a couple lines that don't belong anywhere. You can actually accomplish the file merge with a one-liner on the command line...

cat file1 file2 file3 > file4

That'll redirect the contents of file1 file2 and file3 into file4. No need to check the existance of file4, or touch it, or delete it beforehand.

The two lines that don't belong in the script are the chmod line and the "sh <file> file1 ... etc." line. The reason is that chmod makes your script executable. So, if your script is already running, you don't need to make it executable. If it's not executable, you can't run the script directly.

I'm not sure what you're trying to do in that other line, but the shell will see it as 'execute the shell script <file> with arguments file1 file2 file3 <target file>'. To clarify, I literally mean it will see it as '<file>', not 'myactualfilename'.

andysimmons
12-10-2003, 12:14 PM
Hey bud. You actually don't want to start a new thread; it just confuses everyone. Just click on the reply button at the bottom of the old thread (http://www.justlinux.com/forum/showthread.php?s=&postid=670442#post670442) to respond.

HelpPlease
12-10-2003, 04:00 PM
could this work:

#!/bin/sh
if (file4.txt)
cat file1.txt file2.txt file3.txt >> file4.txt
echo "Transfer Complete"

serz
12-10-2003, 05:04 PM
I don't think so, if (file4.txt) what?