If/else is not working. Why?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting If/else is not working. Why?
# 1  
Old 06-09-2009
If/else is not working. Why?

Hi all,

It's my first post on forum - saying HELLO Smilie

I have a simple script and would like to improve it.
When [user] in terminal writing correct file name then designed file making grep, etc.But if [user] writing wrong file name then warning should appear in terminal.

This script doesn't work properly. Can you tell me why, please Smilie

Code:
#!/bin/bash 

##############################################
#Script is taking C,E,V records from P1 file
#
#############################################

#Direction of P1 file
P1_DIR='/home/sprint/Desktop/jacek/test/'
CEV_DIR='/home/sprint/Desktop/jacek/test/cev/'
ERR1="File does not exist - try again with correct file number"

#Some description how it works
echo ""
echo "Hello!"
echo ""
echo "Script will create C, E, V records from P1 file"
echo ""
echo "Type file name without prefix & extension - example 123456023A"
echo ""

read filename

cd $P1_DIR

#It's taking H,C,E,V records from P1 file
if [ -f NSR$filname.p190 ]; then

#It's taking ...
grep ^H NSR$filename.p190 > hdr 
grep ^C NSR$filename.p190 > Crec 
grep ^E NSR$filename.p190 > Erec 
grep ^V NSR$filename.p190 > Vrec 

#it's creating E,V,C records and save as we want :)
cat hdr Erec > "NSR${filename}_Erec.p190" 
cat hdr Vrec > "NSR${filename}_Vrec.p190" 
cat hdr Crec > "NSR${filename}_Crec.p190" 

#cd $P1_DIR
mv $P1_DIR"NSR${filename}_Erec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Vrec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Crec.p190" $CEV_DIR 

echo ""


else
    echo "$ERR1"
    exit 1
fi

# 2  
Old 06-09-2009
What kind of error are you getting or is it just always performing the else part of the if statement. What is the name of the file you are typing in? Can you do an ls -l on the file your are trying to parse?
# 3  
Old 06-09-2009
Try to echo the value of NSR$filname.p190 and see if it matches with the file name that
exists on the directory


-Devaraj Takhellambam
# 4  
Old 06-09-2009
Lightbulb

Let's start from the beginingSmilie

Folder P1_DIR contain files for example: NSR12345001A.p190, NSR11232002A.p190, NSR54545003A.p190, etc.;

When I writing in terminal ./script then on screen appear:
Code:
  
Hello!

Script will create C, E, V records from P1 file

Type file name without prefix & extension - example 123456023A

After it I have to type file for example 54545003A --> if file is exist the
script will do
Code:
grep ^H NSR$filename.p190 > hdr 
grep ^C NSR$filename.p190 > Crec 
grep ^E NSR$filename.p190 > Erec 
grep ^V NSR$filename.p190 > Vrec 

#it's creating E,V,C records and save as we want :)
cat hdr Erec > "NSR${filename}_Erec.p190" 
cat hdr Vrec > "NSR${filename}_Vrec.p190" 
cat hdr Crec > "NSR${filename}_Crec.p190" 

#cd $P1_DIR
mv $P1_DIR"NSR${filename}_Erec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Vrec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Crec.p190" $CEV_DIR

and showing message "...done"
but
if file doesn't exist the in terminal should appear message
"File does not exist - try again with correct file number".

Script is working with version
Code:
#!/bin/bash 

##############################################
#Script is taking C,E,V record from P1 file
#
#############################################

#Direction of P1 file
P1_DIR='/home/sprint/Desktop/jacek/test/'
CEV_DIR='/home/sprint/Desktop/jacek/test/cev/'
ERR1="File does not exist - try again with correct file number"

#Some description how it works
echo ""
echo "Hello!"
echo ""
echo "Script will create C, E, V records from P1 file"
echo ""
echo "Type file name without prefix & extension - example 123456023A"
echo ""

read filename

cd $P1_DIR

#It's taking H,C,E,V records from P1 file


#It's taking ...
grep ^H NSR$filename.p190 > hdr 
grep ^C NSR$filename.p190 > Crec 
grep ^E NSR$filename.p190 > Erec 
grep ^V NSR$filename.p190 > Vrec 

#it's creating E,V,C records and save as we want :)
cat hdr Erec > "NSR${filename}_Erec.p190" 
cat hdr Vrec > "NSR${filename}_Vrec.p190" 
cat hdr Crec > "NSR${filename}_Crec.p190" 

#cd $P1_DIR
mv $P1_DIR"NSR${filename}_Erec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Vrec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Crec.p190" $CEV_DIR

I would like to insert if/else because it's good idea to prompt that somebody did't write correct filname.

What do you thing abut it?

c
# 5  
Old 06-09-2009
Your if statement below looked okay. Just wondering why you said it wasn't working. You used the -f test to check if the file is a regular file. You may also try a -e to see if the file exists.
Code:
#!/bin/bash 

##############################################
#Script is taking C,E,V records from P1 file
#
#############################################

#Direction of P1 file
P1_DIR='/home/sprint/Desktop/jacek/test/'
CEV_DIR='/home/sprint/Desktop/jacek/test/cev/'
ERR1="File does not exist - try again with correct file number"

#Some description how it works
echo ""
echo "Hello!"
echo ""
echo "Script will create C, E, V records from P1 file"
echo ""
echo "Type file name without prefix & extension - example 123456023A"
echo ""

read filename

cd $P1_DIR

#It's taking H,C,E,V records from P1 file
if [ -f NSR$filname.p190 ]; then

#It's taking ...
grep ^H NSR$filename.p190 > hdr 
grep ^C NSR$filename.p190 > Crec 
grep ^E NSR$filename.p190 > Erec 
grep ^V NSR$filename.p190 > Vrec 

#it's creating E,V,C records and save as we want :)
cat hdr Erec > "NSR${filename}_Erec.p190" 
cat hdr Vrec > "NSR${filename}_Vrec.p190" 
cat hdr Crec > "NSR${filename}_Crec.p190" 

#cd $P1_DIR
mv $P1_DIR"NSR${filename}_Erec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Vrec.p190" $CEV_DIR 
mv $P1_DIR"NSR${filename}_Crec.p190" $CEV_DIR 

echo ""


else
    echo "$ERR1"
    exit 1
fi

# 6  
Old 06-09-2009
Thanks a lot for a help and very quick answearSmilie

c

-----------------------
Edition:

Hi all,

was
Code:
if [ -f NSR$filname.p190 ]; then

should be
Code:
if [ -f NSR$filename.p190 ]; then

One more time thanks
c

Last edited by climbwave; 06-09-2009 at 04:35 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. Shell Programming and Scripting

Working web service call not working with curl

Hello, Newbie here, I have a perfectly well working web service call I can issue from chrome (PC Windows 10) and get the results I want (a dimmer being turned on in Fibaro Home Center 2 at level 40) I am not allowed to post urls but the below works with http and :// and... (3 Replies)
Discussion started by: abigbear
3 Replies

3. Shell Programming and Scripting

PHP cronjob not working but manual working

Hi, Can anyone help me on my PHP cron not working, but when i do the manual it work. # manual run working /usr/local/bin/php /root/dev/test.php # crontab not working 55 8 * * * /usr/local/bin/php /root/dev/test.php Thank in advances Regards, FSPalero Please use CODE tags as... (2 Replies)
Discussion started by: fspalero
2 Replies

4. Shell Programming and Scripting

Automating pbrun /bin/su not working, whenever manually it is working using putty

I am trying to automate a script where I need to use pbrun /bin/su but for some reason it is not passing thru the pbrun as my code below. . ~/.bash_profile pbrun /bin/su - content c h 1 hpsvn up file path I am executing this from an external .sh file that is pointing to this scripts file... (14 Replies)
Discussion started by: jorgejac
14 Replies

5. Red Hat

Nslookup working but ping not working at windows client

Hi Team we have created a DNS server at RHEL6.2 environment in 10.20.203.x/24 network. Everything is going well on linux client as nslookup, ping by host etc in entire subnet. We are getting problem in windows client as nslookup working as well but not ping. all the firewall is disabled and... (5 Replies)
Discussion started by: boby.kumar
5 Replies

6. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

7. Solaris

SSH: internal working but external not working

Hi, This is a strange issue: We have an sftp server. Users can ssh to it from internal LAN without any issue, but they can not ssh to it externally via firewall. Here is what I got: OS is Solaris 9. No hosts.allow and hosts.deny files. Please help. Thank you in advance! (7 Replies)
Discussion started by: aixlover
7 Replies

8. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

9. Linux

FTP not working under Linux but working under any other OS ??? Very strange

Dear all, I am totally despaired and puzzled. Using Filezilla under Windows under the same network as our Linux servers is working. Using FTP command-line client under any of our Linux debian servers is not working ! I tried with different FTP servers -> same problem ! All commands are... (12 Replies)
Discussion started by: magix_ch
12 Replies

10. Solaris

GUI not working... CLI is working fine

Hello, I have X4500 running Solaris 10. I can access it through CLI but I cannot see the GUI. When I reboot it, the GUI works till all the files are loaded (ie., the initial boot sequence) and it prompts me to enter username and password and there it ends. The screen just has a blinking cursor... (4 Replies)
Discussion started by: bharu_sri
4 Replies
Login or Register to Ask a Question