need help with script

Discussion in 'Mac Programming' started by twoodcc, Oct 26, 2008.

  1. twoodcc macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #1
    ok, so what i want to do is make some sort of script, either applescript or shell script, that will do the following:

    take out from a command in the terminal
    put that output into a text file already on my computer.

    the only thing is that i need to put the output in the file kinda weird: i need to take it and put each character of output on a new line, but not create new lines

    example:

    take output of command 'uptime' (up 32 days, 23:09)
    place in file 'myFile'

    output:
    ...
    key u
    key p
    key space
    key 3
    key 2
    key space
    key d
    ...

    anyone have any idea on how to do this?

    thanks in advance
     
  2. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #2
    This sounds like a homework assignment. Whatever.


    Code:
    import sys
    from subprocess import *
    def spaceify(str):
    	output = ""
    	i = 0
    	while i < len(str):
    		if str[i] != "\n":
    			output += str[i] + "\n"
    		else:
    			output += str[i] 
    		i+=1
    	return output
    
    pipe = Popen(sys.argv[1], shell=True, stdout=PIPE).stdout
    outfile = open(sys.argv[2],"a")
    ln = pipe.readline()
    while ln != "":
    	outfile.write(spaceify(ln))
    	ln = pipe.readline()
    outfile.close()
    
    Save that code into a new file with a .py extension

    Invoke it in the shell as:

    Code:
    python <name saved file>.py <command to invoke> <name of file to save in>
    
    If the command you're invoking has extra flags, enclose it in quotations.

    Now give me $20.
     
  3. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #3
    thank you for the reply!

    i ran this script, and it did write to the file, but not quite was i was looking for. it put the output of the command at the bottom of the file, one character on each line.

    what i need is to add "key " to each line of output from the command, and place that output in the file starting at "key n" where n = some number 0-9.

    so basically, the what the script that i ran did was this:

    u

    p

    t

    i

    m

    e

    but what i wanted was this:

    key u
    key p
    key t
    key i
    key m
    key e

    and play that when you get to "key 0" or key "1" or "key 2" etc
     
  4. SC68Cal macrumors 68000

    Joined:
    Feb 23, 2006
    #4
    What is this script for? This really has no real-world value so it must be some sort of homework assignment. Is this supposed to be a histogram of letters used?
     
  5. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #5
    it is not a homework assignment. it's something that i'm trying to experiment with. i also am trying to use the terminal more, and would like to learn shell scripting
     
  6. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #6
    here is what someone else made, and i altered a little bit:

    Code:
    #!/bin/bash
    
    output=$(uptime)
    length=${#output}
    
    for ((i=1; i<=length; i++))
    do
    character=$(echo $output | cut -c$i)
    case "$character" in
    [[:space:]]) echo "key <space>";;
    *) echo "key $character";;
    esac
    done
    now this does exactly what i want, except it just prints to the screen. so how can i make this write to a file?
     
  7. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #7
    would i just put " >> filename" after the statements to write to the file? i did this, and it worked, but it writes to the bottom of the file.

    so how would i write to a certain part in the file?

    i've been trying to figure out if statements for unix scripts, but am having a little trouble
     
  8. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #8
    well i have been messing around, and here is what i have so far:

    Code:
    #!/bin/bash
    
    output=$(uptime)
    length=${#output}
    
    cat tim | while read LINE
    do
    if [LINE == "key 0"]; then
    for ((i=1; i<=length; i++))
    do
    character=$(echo $output | cut -c$i)
    case "$character" in
    [[:space:]]) echo "key <space>" >> tim;;
    *) echo "key $character" >> tim;;
    esac
    done
    
    fi
    done
    the file name is 'tim'. but i get an error on line 8 with the if statement. what am i doing wrong?
     
  9. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #9
    okay, i am getting somewhere. this is the code that i have now:

    Code:
    #!/bin/bash
    
    output=$(uptime)
    length=${#output}
    
    cat tim | while read LINE
    do
    if [ "${LINE}" == "key 0" ]; then
    for ((i=1; i<=length; i++))
    do
    character=$(echo $output | cut -c$i)
    case "$character" in
    [[:space:]]) echo "key <space>" >> tim;;
    *) echo "key $character" >> tim;;
    esac
    done
    
    fi
    done
    now, this finds every line in the file that is "key 0", but then writes the output at the bottom of the file. it does this for every "key 0". my logic here is wrong.

    what i want: i have a block of text that i want to replace with this output. starting with the first line that is "key 0", and write the block of code once, and stop
     
  10. AUFan macrumors newbie

    Joined:
    Feb 2, 2008
    #10
    If you want to write to a new file or overwrite a new file (via terminal) you would do

    [terminal commands] > filename

    Code:
    ls -l -a > fileslist
    
    That takes the long listing of every single file or subdirectory in whatever directory you are located in and instead of displaying the output in the terminal dumps it into a new file called fileslist that you could then open with something like TextEdit. (It will also overwrite existing files if you happen to use the same filename as some other file in the same directory)

    Code:
    ls -l -a >> fileslist
    
    This does the same thing as the prior section but instead of overwriting an existing file it appends it (the content is added to the end of the existing file).
     
  11. twoodcc thread starter macrumors P6

    twoodcc

    Joined:
    Feb 3, 2005
    Location:
    Right side of wrong
    #11
    thanks for the reply. i know this command, but this doesn't allow me to overwrite lines in the middle of the file.
     

Share This Page