Find the answer to your Linux question:
Results 1 to 10 of 10
Code: #!/bin/bash #script name = mkarray if [ $# -lt 2 ]; then tempstr="$1" else tempstr="$1" while [ $# -gt 1 ]; do shift tempstr="$tempstr\ $1" done fi cat -ns ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586

    BASH script problem


    Code:
    #!/bin/bash
    #script name = mkarray
    	if [ $# -lt 2 ]; then
    		tempstr="$1"
    	else
    		tempstr="$1"
    		while [ $# -gt 1 ]; do
    			shift
    			tempstr="$tempstr\ $1"
    		done
    	fi
    
    	cat -ns ~/.flashcards/"$tempstr" > ~/.flashcards/.temparrays
    when i run this script with a 2 word function i get this:
    Code:
    [user@localhost ~]$ mkarray another test
    cat: /home/josh/.flashcards/another\ test: No such file or directoryp
    this is being run as a function in another script so i really can't change the code too much, i just need help understanding exactly what's wrong and a way to fix it

    Thanks in advance!!

  2. #2
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    Within double quotes, the backslash loses its special meaning when preceding meaningless characters. What you want is probably this:
    Code:
    tempstr = "$tempstr $1"
    Without the backslash, that is.

    However, you could just do this:
    Code:
    #!/bin/bash
    cat -ns ~/.flashcards/"$*" >~/.flashcards/.temparrays

  3. #3
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586

    Red face

    Quote Originally Posted by Dolda2000
    Within double quotes, the backslash loses its special meaning when preceding meaningless characters. What you want is probably this:
    Code:
    tempstr = "$tempstr $1"
    Without the backslash, that is.
    haha...yep that's exactly what it was. i was just getting frustrated having this (minor) problem and wasn't seeing the obvious (i tried complicating things for seemingly no reason)

    thanks a lot...it's working correctly now

  4. #4
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586
    ok, i've got that resolved, but i've run into another problem with this script (elsewhere and unrelated to the last). the script searches for a number using "grep -w $1"--where $1 is the name of the file--(supposed to be only the first number on the line) then adds the rest of the line (excluding the first number on the line) to an array, where it'll later be printed out.

    i have this file:
    Code:
         1  #card set used for flashcards
         2  behavior genetics
         3  field that seeks to discover the influence of hereditary and environmental factors on individual differences in human traits and development
         4  Erikson's Stages (8)
         5  trust v mistrust (1st year); autonomy v shame & doubt (1-3 years); initiative v guilt (3-5 years); industry v inferiority (6-puberty); identity v identity confusion (10-20 years); intimacy v isolation (20s & 30s); generativity v stagnation (40s & 50s); integrity v desparity (60s ->)
    i'm trying to find a simple way where i can search for and find only the first number of each line.

    The problem is that the script finds every search-number then outputs all lines containing that number. say, for example, i search for "2" and the script searches the above file:
    Code:
    [user@localhost ~]$ searchnum 2
    2  behavior genetics  5  trust v mistrust (1st year); autonomy v shame & doubt (1-3 years); initiative v guilt (3-5 years); industry v inferiority (6-puberty); identity v identity confusion (10-20 years); intimacy v isolation (20s & 30s); generativity v stagnation (40s & 50s); integrity v desparity (60s ->)
    it searches for any line containing "2" no matter where the "2" might be. how would i have it search only for the "2" at the beginning of the line, and echo what's after the number? (i understand how to print what's after, now...but how would i have it only search the first number and not the rest of the line/file??) for some reason, i'm stumped here. i've been trying to find a way to use grep (as this is the only command i know of that would search) but grep seems to do too much for my purposes, and searches the whole file. is there a command that will search line-by-line or do what i want?

    **Alternatively, a command that could output the contents of a text's single line (based on number or otherwise)...or line-by-line sequentially, would be very useful here also. but, once again, i don't know of such a command but I'd really like to be enlightened of one if there is such a command

    Help is greatly appreciated!

    I'm writing a flashcard study program and this seems to be the last bug i've run into, but it's quite a bug (terms/definitons will occassionally print out very oddly with way too much).

    I'll post the program once I get some of the bugs worked out


    EDIT:
    hmm...i've been doing some research and it seems like "exec" does what i want. i'll give it a try and see how it goes. any more input is still appreciated

  5. #5
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586

    Cool

    ok, i got it resolved using exec

    i stopped using the numbering technique (through "cat -ns $1") and have switched to this instead. it's simpler, efficient, and saved a lot of extra code

    here's a great site with some info on how to use the command "exec" within a script: http://linuxreviews.org/beginner/abs...en/c12753.html

    this is another technique that i had always thought was way over my head, but i'm proud to say i've used it functionally and i now understand it better

  6. #6
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586

    one more random BASH scripting question...

    let's say there's a command like this, with the script called "breakoption":
    Code:
    breakoption 1 2 "3 'echo this is a test'" 4 5
    i have it set up so that it scrolls through the numbers, but how would i get it to execute the command following the number 3 once it gets to that number (while scrolling through the "options" using the "shift" command)?

    does that make sense? if the user gave that input in a console, the expected output would be "this is a test", since the command after the 3 would be "echo this is a test"

    Thanks in advance!


    EDIT: it seems like "sed" could possibly be used here, but how would i only edit out the first number/character? i have no experience with "sed" this is why i ask

  7. #7
    Linux Guru
    Join Date
    Oct 2001
    Location
    Täby, Sweden
    Posts
    7,578
    I'm not sure I understand exactly what you mean, but could you be referring to something like this?
    Code:
    for arg in "$@"; do
        cmd = sed -n "s/^[0-9]\+ \+'\(.*\)'$/\1/p"
        if [ -n "$cmd" ]; then eval "$cmd"; fi
    done

  8. #8
    Linux User muha's Avatar
    Join Date
    Jan 2006
    Posts
    290
    since you are dealing with colums, sounds to me like you'd want to use awk:
    Code:
    awk '{ print $2, $1 }' textfile.txt
    Print the two first columns from a textfile in reverse order.

    Or do you want to detect command line options: http://www.linuxcommand.org/wss0130.php#detecting
    Code:
    while [ "$1" != "" ]; do
        case $1 in
            -f | --file )           shift
                                    filename=$1
                                    ;;
            -i | --interactive )    interactive=1
                                    ;;
            -h | --help )           usage
                                    exit
                                    ;;
            * )                     usage
                                    exit 1
        esac
        shift
    done
    (usage is a function, not a normal command)

    Maybe you could try explaining what you are trying to get working cause i don't get it

  9. #9
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586

    I'll try to explain it further for you...thank you so much for your help!

    well, this is the module from the program i'm writing (a flashcard program) that reads input from a file (containing the menu and each item's action):
    Code:
    print()
    {
    	exec 20<&0
    
    	exec < ~/.flashcards/.menu
    
    	read temp
    	echo -e $BOLD"$temp $END"
    	read temp
    	count=1
    	while [ "$temp" ]; do
    		if [ "$temp" = "-" ]; then
    			echo ""
    			read temp
    		else
    			if [ "$1" = "$count bold"  ]; then
    				echo -e "$count -$BOLD $temp $END"
    				read temp
    				action[$count]=""
    				shift
    				read temp
    			elif [ "$1" = "$count" ]; then
    				echo -e $LIGHT_GRAY"$count - $temp $END"
    				read temp
    				action[$count]=""
    				shift
    				exitprog="disabled"
    				read temp
    			elif [ !"$1" ]; then
    				echo "$count - $temp"
    				read temp
    				action[$count]="$temp"
    				read temp
    			else
    				$INSERT WHAT'S NEEDED HERE
    			fi
    			count=$((count+1))
    		fi
    	done
    
    	echo ""
    
    	if [ "$exitprog" = "disabled" ]; then
    		echo -e $LIGHT_GRAY"0 - Exit program $END"
    	else
    		echo "0 - Exit program"
    	fi
    
    	echo ""
    	echo -e $RED"Currently selected flashcard set:$END $currentset"
    	echo ""
    
    	exec 0<&20 20<&-
    
    	echo -n "Enter selection: "
    }
    module that makes the file:
    Code:
    menufile()
    {
    		echo "FLASHCARDS - Program Menu" > ~/.flashcards/.menu
    		echo "-" >> ~/.flashcards/.menu
    		echo "List of available flashcards" >> ~/.flashcards/.menu
    		echo "display" >> ~/.flashcards/.menu
    		echo "Select flashcards to use" >> ~/.flashcards/.menu
    		echo "choose" >> ~/.flashcards/.menu
    		echo "-" >> ~/.flashcards/.menu
    		echo "Study current flashcards" >> ~/.flashcards/.menu
    		echo "study" >> ~/.flashcards/.menu
    		echo "Search for a flashcard in this set" >> ~/.flashcards/.menu
    		echo "search" >> ~/.flashcards/.menu
    		echo "Add a flashcard to this set" >> ~/.flashcards/.menu
    		echo "add" >> ~/.flashcards/.menu
    		echo "Delete a flashcard from this set" >> ~/.flashcards/.menu
    		echo "del" >> ~/.flashcards/.menu
    		echo "-" >> ~/.flashcards/.menu
    		echo "Create a new flashcard set" >> ~/.flashcards/.menu
    		echo "addset" >> ~/.flashcards/.menu
    		echo "Delete an existing flashcard set" >> ~/.flashcards/.menu
    		echo "delset" >> ~/.flashcards/.menu
    }
    this is what i have that's related to the command i'm asking for. the part in the first module that's commented out is what i'd like made. the best way for me to explain what i need it to do is by example, though.

    i want this:
    Code:
    print 1 2 "3 echo 'this is a test'"
    to execute the command:
    Code:
    echo 'this is a test'
    when the user inputs the number 3 (the command will substitute the command originally assigned to "3" by replacing whatever is originally in the "action" array at action[3] with "echo 'this is a test'", like this:
    Code:
    action[$count]="echo 'this is a test'"
    where $count would be 3

    the "1" and "2" following the "print" command, in this case, would be grayed out and their commands would do nothing (but that works perfectly fine).

    Is that a little clearer? if you need any more info, please ask

    thanks in advance for any and all replies!

    and i'd appreciate it if you could explain the code you give me some. i tried dolda's code from earlier but got this odd error...something about the "s" being "unterminated"?? i would have fixed it if i knew a little more about sed , but that may very well have been waht i was looking for (i just tried modifying it so the command would be a string in the "action" array).

  10. #10
    Linux Enthusiast
    Join Date
    Jun 2005
    Location
    Odessa, FL
    Posts
    586
    ok...no need to help on this one any more. i found a way to finish the program without needing to do what i asked help on.

    if anyone is still interested in finding out how to do this, i'm still interested in how you might but it's not that important now. i might incorporate it later if someone finds a way to do this though (as this way is more flexible than what i'm doing now...and it may become important in the future).

    thanks for all of your replies!

Posting Permissions

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