Find the answer to your Linux question:
Results 1 to 5 of 5
So I learned basic bash a while back, and decided to make my own version of touch. It basically makes a file for me in my directory, and inputs all ...
  1. #1
    Just Joined!
    Join Date
    Nov 2010
    Location
    Alaska
    Posts
    55

    [SOLVED] Bash issue (long but probably obvious)

    So I learned basic bash a while back, and decided to make my own version of touch. It basically makes a file for me in my directory, and inputs all of the "#!/bin/bash/" stuff for me in bash, C++, Java, and I assembly (I think) files. Anyways, I'm trying to add a feature so that I can add functions to C++ and Java files when making the file, and I'm having issues with some loops and arrays. I've been playing with it for a couple of hours and can't figure it out.

    The problem (from adding a function to C++file):
    Code:
    	addb=0  #declared somewhere prior
    	echo "How many arguments?"
    	read arg
    	until [ "$addb" = "$arg" ]; do
    		if [ "$addb" = 0 ]; then
    			echo "What is your argument?  Enter in the form of \"type arg\"."
    		else					
    			echo "What is your argument?"
    		fi
    		read input
    		let addb=addb+1
    		if [ "$addb" != "$arg" ]; then
    			let argarray[$addb]=$input #Want to add a comma here, but gets compiler error
    		else
    			let argarray[$addb]=$input
    		fi
    	done
    	addb=0
    	argar=""  # Messed this loop up a lot trying to fix it.  Completely gave up.
    	for n in 1 $arg; do
    		if [ "$n" = "1" ]; then
    			let argar=${argarray[$n]}
    		else
    			let argar=$argar${argarray[$n]}
    		fi
    	done
    Entire code thus far:
    Code:
    #!/bin/bash
    
    pwd=p
    addb=1
    arg=0
    echo "Type the name of your file.  Do not include the file extention."
    read filename
    echo "Type the file extention.  Do not include a dot."
    read ext
    file=$filename"."$ext
    if [ -f "$file" ]; then
    	echo "This file already exists, would you like to overwrite?"
    	read save
    	if [ "$save" = "yes" -o "$save" = "y" ]; then
    		rm $file
    	else
    		echo "Then please try again.  This program will now exit."
    		exit 0
    	fi
    fi
    case $ext in
    	bash) echo "#!/bin/bash" > $fil
    	echo "" >> $file;;
    	java) touch $file
    	echo "Would you like to add anything to the file?"
    	read add
    	if [ "$add" = "yes" -o "$add" = "y" ]; then
    		let addb=0
    		echo "Would you like to import any java files?"
    		read add
    		if [ "$add" = "yes" -o "$add" = "y" ]; then
    			echo "Type stop to end.  Enter in the form of \"java.blah.blah\"."
    			until [ "$add" = "stop" ]; do
    				echo "What class would you like to import?"
    				read add
    				if [ "$add" != "stop" ]; then
    				echo "import "$add";" >> $file
    				fi
    			done
    			echo "" >> $file
    		fi
    	fi
    	echo "public class $name {" >> $file
    	echo "	public static void main(String[] args) {" >> $file
    	if [ "$addb" = 0 ]; then
    		echo "Would you like to declare any functions?"
    		read add
    		if [ "$add" = "yes" -o "$add" = "y" ]; then
    			echo "not yet done"
    		fi
    	fi
    	echo "		" >> $file;;
    	cpp)  touch $file
    	echo "Would you like to add anything to the file?"
    	read add
    	if [ "$add" = "yes" -o "$add" = "y" ]; then
    		addb=0
    		echo "Would you like to include any files? (iostream is already added)"
    		read add
    		if [ "$add" = "yes" -o "$add" = "y" ]; then
    			echo "Type stop to end.  Enter in the form of \"blah\"."
    			until [ "$add" = "stop" ]; do
    				echo "What file would you like to include?"
    				read add
    				if [ "$add" != "stop" ]; then
    				echo "#include <"$add">" >> $file
    				fi
    			done
    		fi
    	fi
    	echo "#include <iostream>" >> $file
    	echo "using namespace std;" >> $file
    	echo "" >> $file
    	if [ "$addb" = 0 ]; then
    		echo "Would you like to declare any functions?"
    		read add
    		if [ "$add" = "yes" -o "$add" = "y" ]; then
    			until [ "$add" = "no" -o "$add" = "n" ]; do
    				echo "What type of function do you want to add?"
    				read type
    				echo "What do you want to name your function?"
    				read funcname
    				echo "How many arguments?"
    				read arg
    				until [ "$addb" = "$arg" ]; do
    					if [ "$addb" = 0 ]; then
    						echo "What is your argument?  Enter in the form of \"type arg\"."
    					else					
    						echo "What is your argument?"
    					fi
    					read input
    					let addb=addb+1
    					if [ "$addb" != "$arg" ]; then
    						let argarray[$addb]=$input
    					else
    						let argarray[$addb]=$input
    					fi
    				done
    				addb=0
    				argar=""
    				for n in 1 $arg; do
    					if [ "$n" = "1" ]; then
    						let argar=${argarray[$n]}
    					else
    						let argar=$argar${argarray[$n]}
    					fi
    				done
    				echo "$type function $funcname("$argar") {" >> $file
    				echo "Would you like to add another function?  You must type n or no to end the loop."
    				read add
    			done
    		fi
    	fi
    	echo "int main () {" >> $file
    	echo "	" >> $file;;
    	s)    echo "main:" > $file
    	echo "" >> $file;;
    	*) touch $file
    esac
    sudo chmod 755 $p$file
    I know it's not pretty of professional looking, but I'm trying to get it to function before I pretty it up.

    Any help would be greatly appreciated.

  2. #2
    Just Joined!
    Join Date
    May 2009
    Location
    Oregon
    Posts
    51
    Looking at your messed up loop, I think what you are trying to do is count over an index, but in bash, it will take a series of objects as a *list* and not a range:

    for example, doing:

    for i in 1 7; do echo $i; done

    returns:
    1
    7

    BUT, if you want it to COUNT from 1 to 7, you need to do it arithmetically:
    for (( i=1; i<7; i=i+1 )); do echo $i; done
    1
    2
    3
    4
    5
    6


    I'm not able to decipher what you mean by "#Want to add a comma here, but gets compiler error"
    So you need to describe what you are trying to do so I can help there.

  3. #3
    Just Joined!
    Join Date
    Nov 2010
    Location
    Alaska
    Posts
    55
    Quote Originally Posted by andrewr View Post
    I'm not able to decipher what you mean by "#Want to add a comma here, but gets compiler error"
    So you need to describe what you are trying to do so I can help there.
    Basically, the syntax for a function in C++ is:
    type function name (type arg1, type arg2) {bla}
    example being:
    int function stuff (int x, int y) {stuff}
    I was trying to save the value of $input with an added comma straight to my array for the sake of ease when it comes to dumping out my arguments, but it gives me a compiler error (Now that I think about it, it gives me a compiler error without the comma). Is there anything illegal to saving the string "int a" to part of an array, or is there something I'm missing?

    Also, thanks for help on the loop thing, I haven't gotten to try it yet, but I'm about to.

    EDIT: It works fine without the comma, it's only when I try to add it that it gives me a compiler error.

    EDIT EDIT: The loop works, but it's giving me an an error on
    "let argar=$argar${argarray[$n]}" within the for loop, saying it was expecting an operator. I also declared argar beforehand as "", thought it's probably not necessary.
    Last edited by MisterDood; 05-16-2011 at 11:14 PM.

  4. #4
    Just Joined!
    Join Date
    May 2009
    Location
    Oregon
    Posts
    51
    So, you are wanting to append a comma to the end of an array *string*?

    That is normally done by quoting, and NOT using "let". The let command seems to be a cue to bash that the argument is an arithmetic expression/number; a comma may be an operator -- hence the compile error.

    So I am guessing that what you really want to do is just:

    argarray[$addb]="${input},"

    The curly braces insure the name input is recognized inside the quotes as a variable, buy they may not be needed. See if that helps...

  5. #5
    Just Joined!
    Join Date
    Nov 2010
    Location
    Alaska
    Posts
    55
    Quote Originally Posted by andrewr View Post
    So, you are wanting to append a comma to the end of an array *string*?

    That is normally done by quoting, and NOT using "let". The let command seems to be a cue to bash that the argument is an arithmetic expression/number; a comma may be an operator -- hence the compile error.

    So I am guessing that what you really want to do is just:

    argarray[$addb]="${input},"

    The curly braces insure the name input is recognized inside the quotes as a variable, buy they may not be needed. See if that helps...
    Turns out it was the let command. I thought that bash had scoped variables, but since this works I guess it doesn't. Anyways, I got it to work just fine now, thank you so much for all of your help.
    Last edited by MisterDood; 05-16-2011 at 11:48 PM. Reason: typo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...