LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-23-2005, 11:43 PM   #1
quan
LQ Newbie
 
Registered: Nov 2005
Posts: 4

Rep: Reputation: 0
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

Last edited by quan; 11-23-2005 at 11:45 PM.
 
Old 11-24-2005, 12:31 AM   #2
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
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..
 
Old 11-24-2005, 06:02 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 11-24-2005, 07:53 PM   #4
quan
LQ Newbie
 
Registered: Nov 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 11-24-2005, 08:12 PM   #5
quan
LQ Newbie
 
Registered: Nov 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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
 
Old 11-24-2005, 10:43 PM   #6
wym
LQ Newbie
 
Registered: Oct 2005
Location: Toronto
Distribution: Red Hat 8.0
Posts: 26

Rep: Reputation: 15
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.
 
Old 11-25-2005, 01:27 AM   #7
Artanicus
Member
 
Registered: Jan 2005
Location: Finland
Distribution: Ubuntu, Debian, Gentoo, Slackware
Posts: 827

Rep: Reputation: 31
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.. (:
 
Old 11-25-2005, 03:59 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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> !)
 
Old 11-27-2005, 07:46 PM   #9
quan
LQ Newbie
 
Registered: Nov 2005
Posts: 4

Original Poster
Rep: Reputation: 0
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

Last edited by quan; 11-27-2005 at 08:32 PM.
 
  


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
shell programming qerf Linux - Newbie 5 11-06-2005 05:28 PM
programming shell tuls Slackware 3 08-24-2005 05:58 PM
new to shell programming bluetwist Debian 3 08-01-2005 03:56 PM
Shell Programming eantoranz Linux - Networking 2 06-02-2005 11:21 AM
shell programming shadowsurfer Linux - Newbie 3 09-09-2004 09:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:50 AM.

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
Open Source Consulting | Domain Registration