Problems in shell script if sed is used


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Problems in shell script if sed is used
# 1  
Old 11-12-2010
Problems in shell script if sed is used

Hi All,

Below is the script which i have written in cygwin:
Code:
 
#!/usr/bin/sh
fname=$1
cat $fname | sed 's/ //g' > fname1
for i in `cat $fname1`
do
echo $i > file1
#param1 is script name
param1=`awk -F , '{print $1}' file1`
param1="$param1.sql"
#param2 is BL
param2=`awk -F , '{print $2}' file1`
#param3 is folder name
param3=`awk -F , '{print $3}' file1`
#create config spec file
echo "element * $param2" > cs.txt
echo "element * /main/LATEST" >> cs.txt
cat cs.txt
#Map UCM drive
cd /cygdrive/m/ht62_ucm/GIS_COMP
cleartool setcs -tag ht62_ucm d:/prj/cs.txt
echo set config spec status is $?
fscript1=`cleartool find ./$param3 -element "lbtype_sub($param2)" -name $param1 -print`
if [ "$fscript1" = "" ]
then
echo script $param1 not found
else
echo script $param1 is found
fi
fscript2=`echo "$fscript1" | sed 's/@@//g'`
echo copying file...
cp $fscript2 d:/prj/
cd d:/prj
done

This code works without the line
cat $fname | sed 's/ //g' > fname1...But adding this line makes the program hang..Can anyone help me out?
# 2  
Old 11-12-2010
Hi.

Does it only "hang" when you pass no parameters to the script (that's what I would expect)?

In any case, cat is not necessary:

Code:
sed 's/ //g' "$fname" > fname1

This User Gave Thanks to Scott For This Post:
# 3  
Old 11-12-2010
Thanks a lot scott. It hangs even if i pass parameter...
I will try with the sed example that u have given...
# 4  
Old 11-12-2010
Quote:
Originally Posted by janardhanamk
Code:
 
#!/usr/bin/sh
fname=$1
cat $fname | sed 's/ //g' > fname1
for i in `cat $fname1`
do

This code works without the line
cat $fname | sed 's/ //g' > fname1...But adding this line makes the program hang..Can anyone help me out?
I think your problem lies in the highlighted red text above. You are creating a file called fname1, but trying to cat $fname1. Since $fname1 is not set, cat blocks for input from stdin.
# 5  
Old 11-12-2010
Code:
for i in `cat $fname1`
do
echo $i > file1
...
done

This is a combo useless use of cat and useless use of backticks. It's more efficient, faster, and much less error-prone to do this:

Code:
while read i
do
...
done < file1

...since it reads it line by line instead of trying to cram the entire file at once into the shell. Backticks have size limits, this does not.
# 6  
Old 11-13-2010
Yes, it works if i change "for" loop to "while" loop..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed problems - Bash Script

Hi I keep getting the following error sed: -e expression #1, char 32: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command sed: -e expression #1, char 35: unterminated `s' command whenever I use the following bash script #! /bin/bash... (2 Replies)
Discussion started by: spbr
2 Replies

2. UNIX for Beginners Questions & Answers

Problems executing an interactive shell script

I am new to Unix shell and to this forum. I am having some trouble executing an interactive shell script that I have written using Mac TextEdit that takes a user input via terminal of a file type (jpg or gif) and then activates a script that will iterate through a folder of unsorted file types... (4 Replies)
Discussion started by: Braveheart
4 Replies

3. Shell Programming and Scripting

Shell Script Problems

Hi, I'm newbie in the shell script world and i want to solve some problems I am experiencing. My main goal is to create a ksh script to use 2 text files as input to execute a local shell script(create user commands in the criar.users.aix file) through a ssh connection in which the list of... (1 Reply)
Discussion started by: joaochambino
1 Replies

4. AIX

executable problems with shell script in IBM servers

We have a java stand alone application running currently on sun Solaris system. The java application runs on Jdk 1.4. We are reshooting this java application to new Ibm servers. There are 10 unix scripts for this application. All scripts works well except one shell script, This shell... (2 Replies)
Discussion started by: poojagupta
2 Replies

5. Shell Programming and Scripting

Remotely Executing Shell Script - Problems

Hey Lads, I have a shell script on a remote Server X that i need to execute from Server A. the script executes fine locally but remotely does not. It appears the script on the remote machine is calling another shell script which only has an array defined . Please see below the errors. ... (10 Replies)
Discussion started by: Irishboy24
10 Replies

6. Shell Programming and Scripting

Execution problems with BASH Shell Script

Hi I need help with my coding , first time I'm working with bash . What i must do is check if there is 3 .txt files if there is not 3 of them i must give an error code , if al three is there i must first arrange them in alphabetical order and then take the last word in al 3 of the .txt files... (1 Reply)
Discussion started by: linux newb
1 Replies

7. Shell Programming and Scripting

shell script to call perl script problems

Ok, don't ask me why, but all calls to perl must be called by a shell script. Its really not ideal, but its what I have to work with. Calling it isnt the issue, its passing in the arguments. I have about 1000 perl scripts to call by a shell script. Right now, I'm executing the shell script... (3 Replies)
Discussion started by: regexnub
3 Replies

8. OS X (Apple)

Execution Problems with ASU Shell Script

Hello. I have been trying to create a shell script that checks to see if there are software updates and if not, then exit the script. If so, then check to see if a user is logged in. If a user is logged in, it will only install the updates. If a user is not logged in, then it will display a... (3 Replies)
Discussion started by: Talcon
3 Replies

9. Solaris

Problems with korn shell script

Hey Guys, I'm looking for some advice about a korn shell script I've written. I've spent hours googling for an answer hopefully someone here can help me out. Basically the part of the script I'm having problems with is when I need to SFTP a file from one server to another. The line looks... (6 Replies)
Discussion started by: hilather
6 Replies

10. Shell Programming and Scripting

Shell script problems to do

Does anyone know a good site to do shell script problems? (0 Replies)
Discussion started by: cleansing_flame
0 Replies
Login or Register to Ask a Question