LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell programming (https://www.linuxquestions.org/questions/programming-9/shell-programming-386019/)

quan 11-23-2005 11:43 PM

Shell programming
 
Hi ,

I got this shell script but not sure how to run it.

-------------------------------------------------------------------------------------------------------------------------

Here is how I run it :

1. List all the available shells :
$ cat /etc/shells

This is the result :

/bin/sh
/bin/bash
/sbin/nologin
/bin/ash
/bin/bsh
/bin/ksh
/usr/bin/ksh
/usr/bin/pdksh
/bin/tcsh
/bin/csh
/bin/zsh

2. Run my shell script using the specified shell (my shell script is named "cnvtPcm2Wav")

$ shellname cnvtPcm2Wav Pi16b 44 #(my scipt needs 2 parameters)

WHERE :
shellname is one of the shells listed above (sh , bash , ash , ksh , .......)

-------------------------------------------------------------------------------------------------------------------------

I got the same error with each shell I used AT "setenv".

setenv : command not found

The strange thing is that "setenv" is a UNIX command. Why does my Fedora Core 3 NOT understand it ??????

-------------------------------------------------------------------------------------------------------------------------

This is the content of my script :

#!/bin/csh
#-------------------------------------------------------------------------------
#
# This script takes pcm.hex and a sample rate and converts it to a wave file.
#
#-------------------------------------------------------------------------------

setenv FILENAME $1
setenv FS $2
setenv ofile_ascii $1.wav.ascii
setenv ofile_wave $1.wav
setenv tmpfile_1 pcm_to_wave.tmp1
setenv tmpfile_2 pcm_to_wave.tmp2

#--------------------------------------------------------------------------
#
# Verify that the sample rate option is supported
#
#--------------------------------------------------------------------------

if (($FS != "44") && ($FS != "48") && ($FS != "32")) #then
echo "ERROR : Invalid sample rate"
echo "Should be [44,48,32]"
exit(1)
endif

#--------------------------------------------------------------------------
#
# Part of the wave file header includes information about the file length
# Calculate the file length here and convert it to little endian
# The file length is in bytes.
# Verify pcm.hex has 4 characters per line.
#
#--------------------------------------------------------------------------

set pcm_lines = `cat ${FILENAME} | wc -l`
set pcm_chars = `cat ${FILENAME} | wc -c`
set pcm_bytes = `expr 5 \* ${pcm_lines}`
if ($pcm_bytes != $pcm_chars) then
echo "ERROR : Unrecognized file format. Expecting 4 hex chars per line"
exit(2)
endif

set file_length = `expr 2 \* ${pcm_lines}`
set hex_length = `echo $file_length | d2h -o30`
set byte0 = `echo $hex_length | cut -c7-8`
set byte1 = `echo $hex_length | cut -c5-6`
set byte2 = `echo $hex_length | cut -c3-4`
set byte3 = `echo $hex_length | cut -c1-2`
set lenstr = $byte0$byte1$byte2$byte3

set file_length_p36 = `expr 2 \* ${pcm_lines} + 36`
set hex_length_p36 = `echo $file_length_p36 | d2h -o30`
set byte0_p36 = `echo $hex_length_p36 | cut -c7-8`
set byte1_p36 = `echo $hex_length_p36 | cut -c5-6`
set byte2_p36 = `echo $hex_length_p36 | cut -c3-4`
set byte3_p36 = `echo $hex_length_p36 | cut -c1-2`
set lp36str = $byte0_p36$byte1_p36$byte2_p36$byte3_p36

#--------------------------------------------------------------------------
#
# Part of the wave file header includes information about the sample rate
#
#--------------------------------------------------------------------------

if ($FS == "32") then
set fs_str = "007d0000" # Samples per second
set bs_str = "00f40100" # Bytes per second -- assume 16-bit stereo
else if ($FS == "44") then
set fs_str = "44ac0000"
set bs_str = "10b10200"
else
set fs_str = "80bb0000"
set bs_str = "00ee0200"
endif

#--------------------------------------------------------------------------
#
# Output RIFF header information
#
#--------------------------------------------------------------------------

echo "52494646" >! $ofile_ascii # RIFF 0- 3
echo $lp36str >> $ofile_ascii # length+36 4- 7
echo "57415645" >> $ofile_ascii # WAVE 8-11

#--------------------------------------------------------------------------
#
# Output fmt header information
#
#--------------------------------------------------------------------------

echo "666D7420" >> $ofile_ascii # fmt 0- 3
echo "10000000" >> $ofile_ascii # 4- 7
echo "0100" >> $ofile_ascii # 8- 9
echo "0200" >> $ofile_ascii # stereo 10-11
echo $fs_str >> $ofile_ascii # samples per second 12-15
echo $bs_str >> $ofile_ascii # bytes per second 16-19
echo "04001000" >> $ofile_ascii # 16-bit stereo data 20-23

#--------------------------------------------------------------------------
#
# Output data header information
#
#--------------------------------------------------------------------------
echo "64617461" >> $ofile_ascii # data 0- 3
echo $lenstr >> $ofile_ascii # length 4- 7

#--------------------------------------------------------------------------
#
# Output data section -- convert to little endian data
#
#--------------------------------------------------------------------------

cut -c1-2 $1 >! $tmpfile_1
cut -c3-4 $1 >! $tmpfile_2
paste -d'\0' $tmpfile_2 $tmpfile_1 >> $ofile_ascii
rm $tmpfile_1 $tmpfile_2

#--------------------------------------------------------------------------
#
# Convert to binary
#
#--------------------------------------------------------------------------

cat $ofile_ascii | my_h2b > $ofile_wave
rm $ofile_ascii

-------------------------------------------------------------------------------------------------------------------------

Is this script written for csh ?

How can I make it understand "setenv" ?

Thanks,

Quan

Artanicus 11-24-2005 12:31 AM

setenv is a command, yes. The thing is that its not a binary, but a shell builtin. Now, sofar as much I tested a simple setenv shellscript, it worked just fine, found the builtin setenv, so im not sure what the prblem is..

Make sure you have csh installed and located under /bin/csh and that its not just a symlink to some other shell..

bigearsbilly 11-24-2005 06:02 AM

ok see the first line?

#!/bin/csh

make sure it is the first line, no spaces.

did you copy the file from windoze?
you may have ^M characters.

do
cat -vets | head and see if the first line
looks like

#!/bin/csh^M

if so, you will need to remove the ^M chars.

quan 11-24-2005 07:53 PM

Hi Artanicus ,

I have checked the folder /bin and seen csh there but "setenv" still doesn't work. I really don't know why.

Regards,

Quan

quan 11-24-2005 08:12 PM

Billy reply
 
Hi Billy ,

Your command is great !!!!! I have tried by : cat cnvtPcm2Wav -vets|head

And the result is : #!/bin/csh^M$

I raise 3 questions with this result :

1) Is '^M$' a new line character ?

2) The first line of the file (#!/bin/csh) is a comment line. Is it important whether or not it has ^M$ at the end of the line ?

3) Does the first line hint me that this file is written for a shell (In this case , the shell used for this script is csh) ? If this script is written for csh , why don't I see any C syntax here ?

I look forward to seeing your answer.

Thanks,

Quan

wym 11-24-2005 10:43 PM

in widndows or DOS, any end of line in a text file is 0x0D and 0x0A
but in UNIX, the end of line if just 0x0A,
so when you get a script from windows, you need run:

dos2unix filename

to convert it to UNIX style.

Artanicus 11-25-2005 01:27 AM

the first commented line indeed tells the current shell what program the script wants to be executed in. csh isnt realy that good for shellscripts, but that one seems pretty lowlevel and perhaps then csh is a good choise, or not..

The line indeed is a comment, but it must be the first line, and everything after the ! is take character by character as the app to execute this code, even parameters can be given. So, an extra ^M is pretty destructive in a place like that.. (:

bigearsbilly 11-25-2005 03:59 AM

super!
DOS does CR/LF for new line, unix LF.
and the ^M is a valid part of a file name.
so it may also be messing up the rest of the script.

as wym says, use dos2unix to convert.

#! is not a comment line on the first line, it tells what shell to invoke.
(but is a comment if sourced like: csh <script> !)

quan 11-27-2005 07:46 PM

Dear wym , Artanicus , Billy

Thank you so much for your great explaination. I have tried "dos2unix" and successfully overcome the "setenv" issue. Now I need to write some of the extra scripts to run my program.

Once again , thank you for your help.

Best regards ,

Quan


All times are GMT -5. The time now is 01:09 AM.