Bash code will not run


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash code will not run
# 1  
Old 04-04-2015
Bash code will not run

Why doesn't the code below run? Am I missing something? Thank you Smilie.

Code:
syntax() {
    printf "\n\n"
    printf "Enter HGVS description of variant(s): "; IFS="," read -a hgvs
	
	[ -z "hgvs" ] && printf "\n Nothijng entered. Leaving match function." && sleep 2 && return
        
        for ((i=0; i<${#hgvs[@]}; i++))
              do printf ${hgvs[$i]} >> cd 'C:' c:/Users/cmccabe/Desktop/Python27/syntax.txt
        done 
    esac
}

# 2  
Old 04-04-2015
what happens when you run this code in debug mode ?

add -x in the shebang line
This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 04-04-2015
The bash menu wont open unless that code is removed. Thank you Smilie.
# 4  
Old 04-04-2015
shouldn't it be:
Code:
[ -z "${hgvs}" ]

This User Gave Thanks to vgersh99 For This Post:
# 5  
Old 04-04-2015
This code looks wonky:

Code:
printf ${hgvs[$i]} >> cd 'C:' c:/Users/cmccabe/Desktop/Python27/syntax.txt

What precisely do you expect that to do? I'd expect it to write to the filename 'cd'.
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 04-04-2015
In addition to what vgersh99 and Corona688 said, I have a few concerns with your loop:
Code:
        for ((i=0; i<${#hgvs[@]}; i++))
              do printf ${hgvs[$i]} >> cd 'C:' c:/Users/cmccabe/Desktop/Python27/syntax.txt
        done

  1. Please get used to lining up do and done; it makes it easier to see the structure of your code (especially when you have longer loops).
  2. The printf utility needs a format string. Assuming that user entered data WILL NOT contain any percent signs and WILL contain newlines leads to extremely unreliable output and possible syntax errors.
  3. The >> keyword is a redirection operator; not a command separator.
  4. The command: printf ${hgvs[$i]} >> cd 'C:' c:/Users/cmccabe/Desktop/Python27/syntax.txt (assuming hgvs[$i] does not contain a %) appends the expansion of hgvs[$i] into a file named cd and ignores the other two operands. If hgvs[$i] expanded to %s\n, it would append lines containing C: and c:/Users/cmccabe/Desktop/Python27/syntax.txt to a file named cd.
  5. Even if an array was created using read -a array_name, it is still a good idea to quote expansions of array elements (especially when the setting of IFS is different when running read than it is when expanding the elements, as it is in this case).
Assuming that you are trying to log the elements of your array as separate lines into the log file c:/Users/cmccabe/Desktop/Python27/syntax.txt, you want something like I suggested in one of your other myriad recent threads:
Code:
	for ((i = 0; i < ${#hgvs[@]}; i++))
	do	printf '%s\n' "${hgvs[$i]}" >> c:/Users/cmccabe/Desktop/Python27/syntax.txt
	done

or, more simply,
Code:
	for i in "${hgvs[@]}"
	do	printf '%s\n' "$i" >> c:/Users/cmccabe/Desktop/Python27/syntax.txt
	done

This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

When I run the code yesterday I am getting output,when I run same code today I am getting error?

If run the below code today its creating all directory and getting output files,I f run same code tomorrow I am getting error. can any one give suggestion to sortout this error. OSError: no such file or directory : '062518'My code looks like this import paramiko import sys import os ... (8 Replies)
Discussion started by: haribabu2229
8 Replies

2. Shell Programming and Scripting

How to run several bash commands put in bash command line?

How to run several bash commands put in bash command line without needing and requiring a script file. Because I'm actually a windows guy and new here so for illustration is sort of : $ bash "echo ${PATH} & echo have a nice day!" will do output, for example:... (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Windows & DOS: Issues & Discussions

Run Bash from Windows icon?

Is it possible to run a bash using a by creating an icon or shortcut in windows 7? Everything seems to be for unix or linux and unfortunately my institution is windows. For example, run bash ~/newbatch.sh by clicking on an icon on a users desktop. Cygwin will be installed as well. Thank you... (8 Replies)
Discussion started by: cmccabe
8 Replies

4. Shell Programming and Scripting

Bash if run a command

I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string. This works perfectly. echo $(ipcs | grep Shared | awk '{print $2}')When I put it in an if statement I get some problems. $ if ; then echo expression evaluated... (10 Replies)
Discussion started by: cokedude
10 Replies

5. Shell Programming and Scripting

ssh to run bash script

I want to use ssh to start a bash script that I have uploaded to a webhost. How do I do that from linux? (2 Replies)
Discussion started by: locoroco
2 Replies

6. Shell Programming and Scripting

HOW to run a bash-code in a c-shell script ??

Is there a way to run some code in a C-shell script by different shell, like bash? I have that situation. I have prepared and perfectly workable bash-skript that now I have to execute under C-shell script, divide it on steps and without creating a new files (with the bash-code parts.) For... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Shell Programming and Scripting

How to use perl to run bash with argument?

Hi All, I want to run a bash script using perl. But they are in the different dir. #! /usr/bin/perl -w use strict; my $root=`pwd`; chomp($root); my $cmd=".$root/testdir/ft_623.sh 3 4 5 6 7"; print $cmd; my @line=`$cmd`; foreach (@line){ print $_; } ft_623.sh (0 Replies)
Discussion started by: Damon sine
0 Replies

8. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

9. Shell Programming and Scripting

Automatically run bash script

Hi! I want to run/execute a bash script automatically everytime when a specific file is created or when its timestamp changes. Is this possible? How? Thank you very much for your answers in advance, Regards, Christoph (1 Reply)
Discussion started by: ckofler
1 Replies

10. UNIX for Dummies Questions & Answers

How to use cygwin to run bash script

Hi, all, I try to run a quite simple bash script mytest.sh in cygwin, it's content is: #!/bin/bash echo "It is my first bash shell" there are three lines in the script. The second line is blank line. When I run it use command: bash c:/mytest.sh, ... (6 Replies)
Discussion started by: Jenny.palmy
6 Replies
Login or Register to Ask a Question