Batch or not if/then


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Batch or not if/then
# 1  
Old 10-23-2014
Batch or not if/then

I am having trouble modifying a .sh that allows the user to run different command based of an input. So based on the user input of Y or N a different command is run. Thank you Smilie.

Code:
 #!/bin/bash
while true
do
        printf "Is this a batch  : " ; read id
        [ -z "$id" ] && break
If "$id" = Y, Then
        [ "$id" = "end" ] && break
        perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt 
Else
      If "$id" = N, Then 
      printf "Enter ID  : " ; read id
      [ -z "$id" ] && break
      [ "$id" = "end" ] && break
      perl table_annovar.pl ${id}_matched.avinput humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo
done

# 2  
Old 10-23-2014
Your if's and then's are wrong.

Code:
if [ "$id" = "Y" ]
then
        echo "do something
elif [ "$id" = "N" ]
then
        echo "do something else"
else
        echo "unknown option"
fi

Note that the extra linebreaks aren't optional. You need either a linebreak or ; between if ... and then

But personally I'd do this with case instead:

Code:
case "$id" in
[yY])
        echo "do something"
        ;;
[nN])
        echo "do something else"
        ;;
end)
        break
        ;;
)
        break
        ;;
*)
        echo "Unknown option"
        ;;
esac


Last edited by Corona688; 10-23-2014 at 01:36 PM..
# 3  
Old 10-23-2014
You ought to pay attention to the casing and the proper syntax of the bash shell.
If is not the same that if
Then is not the same that then
Else is not the same that else
Case matters
Code:
If "$id" = Y, Then
        [ "$id" = "end" ] && break

That's not proper syntax for an if condition in bash

The correct syntax could be:
if [ "$id" = Y ]; then

Further more, if the variable $id contains the char Y, how can ever contain the string "end"? Think about that one.

I going to pretend I did not see any of the perl lines.
# 4  
Old 10-23-2014
I am doing something wrong, but not familiar enough yet to debug it properly. Thank you Smilie.

Code:
 #!/bin/bash
while true
do
        printf "Is this a batch  : " ; read id
        [ -z "$id" ] && break
        [ "$id" = "end" ] && break
case "$id" in
[yY])
        echo "perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt"
        ;;
[nN])
        printif "Enter ID : ' ; read id
		[ -z "$id" ] && break
        [ "$id" = "end" ] && break
        ;;
end)
       
	    ;;
)	  

	    ;;
*)
        echo "perl table_annovar.pl ${id}_matched.avinput humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo"
	    ;;
esac
done

Code:
  bash ~/test.sh
/home/cmccabe/test.sh: line 7: syntax error near unexpected token `$'in\r''
'home/cmccabe/test.sh: line 7: `case "$id" in

# 5  
Old 10-23-2014
Quote:
Originally Posted by cmccabe
Code:
  bash ~/test.sh
/home/cmccabe/test.sh: line 7: syntax error near unexpected token `$'in\r''
'home/cmccabe/test.sh: line 7: `case "$id" in

This particular error means "Stop editing your scripts in Microsoft Notepad". Editing it in a Windows editor has filled them with carriage returns. To repair it:

Code:
tr -d '\r' < windowstext > unixtext

Why have you put absolutely everything in the case statement? Most of those statements will never be executed, since it jumps from ;; to esac. Not to mention, you can check for "end" in the same case statement. Try this:

Code:
printf "Is this a batch :"
while read id
do
        case "$id" in
        [yY]) echo perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt 
                ;;
        [nN]) # code for X
                printf "Enter ID  : " ; read id
                [ -z "$id" ] && break
                [ "$id" = "end" ] && break
                echo perl table_annovar.pl ${id}_matched.avinput humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo
                ;;
        end)  break ;;
        *)     ;; # Unknown option, do nothing
        esac

        printf "Is this a batch :"
done


Last edited by Corona688; 10-23-2014 at 04:31 PM..
# 6  
Old 10-23-2014
It's also quite interesting how here, you have a shell script, which calls perl... which calls system (which is another shell!) -- which calls perl again! You could maybe remove two or three levels of code there with no change.

Last edited by Corona688; 10-23-2014 at 04:40 PM..
# 7  
Old 10-23-2014
Code:
 #!/bin/bash
while true
do
        printf "Is this a batch :"
while read id
do
        case "$id" in
        [yY]) echo perl -ne 'chomp; system ("perl table_annovar.pl $_ humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo")' < file.txt 
                ;;
        [nN]) # code for X
                printf "Enter ID  : " ; read id
                [ -z "$id" ] && break
                [ "$id" = "end" ] && break
                echo perl table_annovar.pl ${id}_matched.avinput humandb/ -buildver hg19 -protocol refGene,popfreq_all,common,clinvar,clinvarsubmit,clinvarreference -operation g,f,f,f,f,f -otherinfo
                ;;
        end)  break ;;
        *)     ;; # Unknown option, do nothing
		esac
		
		printf "Is this a batch :"
done

Code:
  bash ~/test.sh
/home/cmccabe/test.sh: line 22: syntax error: unexpected end of file

I have to get out of the habit of using a windows editor (I do that a lot), why is there a second
Code:
 printf "Is this a batch :"

. The program is written in perl and I am using a shell to make it easier for others to use. Thanks Smilie.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Batch code

Hi Friends, I have a script like this cat script #!/bin/bash #Name: name #Task: name #$ -e /path/to/error/logfile/err.log #$ -o /path/to/output/logfile/out.log I have a list of commands like this cat commands cat 1.txt > 2.txt cat 23.bed > 789.alm zcat 1.gz > 1.txt (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. UNIX for Dummies Questions & Answers

SSH batch help

Dear Gurus, I had hundreds of equipments which i can only use SSH to login and retrieve informations. Thus, how should i do that? eg:ssh batch @ echo off ssh <server1>@10.168.1.1 ECHO testing1 exit ssh <server2>@10.168.1.2 ECHO testing2 exit Can the above method used? :) (6 Replies)
Discussion started by: ymeyaw
6 Replies

3. Shell Programming and Scripting

Executing a batch of files within a shell script with option to refire the individual files in batch

Hello everyone. I am new to shell scripting and i am required to create a shell script, the purpose of which i will explain below. I am on a solaris server btw. Before delving into the requirements, i will give youse an overview of what is currently in place and its purpose. ... (2 Replies)
Discussion started by: goddevil
2 Replies

4. Windows & DOS: Issues & Discussions

help with batch script

I have a file named xyz.txt with the contents eg: abc/pluto/tag/ver_1.0(b0123) abc/pippo/tag/ver_1.0(b0124) . and so on I need a script which read one by one the enteries from the file xyz.txt create folders pluto/ver_1.0(b0123) and pippo/ver_1.0(b0124) ans so on and run a command svn... (1 Reply)
Discussion started by: nerd1976
1 Replies

5. UNIX for Advanced & Expert Users

batch file

Hi all I am using tru64 Unix and I want a ready batch file which makes me to change all user passwords at the same time ,instead of changing everyone separately. Please could anyone help me to do that. bye. (1 Reply)
Discussion started by: ahmedbashir
1 Replies

6. Shell Programming and Scripting

batch renaming ...

hi all, given a path, for example : /<pwd>/artist/album/ what i would like to do is to rename the album directory like that : /<pwd>/artist/artist | album/ and i would like to do the latter for all the "artist" directories and for all the "album" directories that belong to an artist ... (4 Replies)
Discussion started by: OneDreamCloser
4 Replies

7. Programming

batch file

what is a command to call a batch file from a c++ program when called with the argument which is a text file, then how to print that text file on a printer. please help me with code if possible (3 Replies)
Discussion started by: ramneek
3 Replies

8. IP Networking

batch file

my requirment is i have to call a batch file from a c++ program that batch file is called with the argument as print.txt print.txt is a text file which should get printed on printer (any printer) when a batch file call is made 1) how to call a batch file in c++ 2) how to print the text... (1 Reply)
Discussion started by: ramneek
1 Replies

9. Shell Programming and Scripting

Using Batch command

Hi All I have a need due time constraint to issue the mail command in background. I have a script that sends a message to a mail address as follows :- echo "$MLINE" | mail -s "$HOST - $TEXT" name@co.com & The script runs frequently and I check to see if the script is active and if it is , it... (5 Replies)
Discussion started by: jhansrod
5 Replies

10. Shell Programming and Scripting

rename in batch

example test1 will have m1234567.12a I would like to rename in batch but I don't Please help me on this. cd /a1/a2/a3 test1=$(basename /a1/a2/a3/*.*) >> /tmp/t echo $test1 echo "Extracting 8 th position" >> /tmp/t2 awk '{print substr($1,8,1); }' $test1 >> /tmp/t3 echo "extraction ... (3 Replies)
Discussion started by: kathy18
3 Replies
Login or Register to Ask a Question