LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-04-2006, 12:34 AM   #1
tangle
Senior Member
 
Registered: Apr 2002
Location: Smithville, TN
Distribution: Slackware
Posts: 1,744

Rep: Reputation: 70
bash syntax when envoking tar command in python script


I get this error when envoking a python script.
Code:
sh: -c: line 2: syntax error near unexpected token `;'
sh: -c: line 2: `; } 2>&1'
The script reads a file and backs up a directory in a text file. This is the script. I put the print commands in it to see if the variables where right.
Code:
import os
import commands
import datetime

def cls():
    # Define the clear windows command
    clear = commands.getoutput('clear')
    print clear

cls()
#Read a file
in_file = open("backup_list.txt","r")
list = in_file.read()
in_file.close()
print "list =", list

# Get username and set user variable
user = os.environ['USER']
print "user =", user

# Set backup command
bk_cmd = "tar cvf "
print "bk_cmd =", bk_cmd

# Set path variable
path = "/home/" + user + "/"
print "path =", path

# Set run_bk variable 
run_bk = bk_cmd + path + user + ".tar" + " " + list
print "run_bk =", run_bk

# Run backup 
output = commands.getoutput(run_bk)
print output
I wote a similiar script with asks for information from the user (it worked perfectly). the only difference is that I used the user = os.environ['USER'] so that the script calls for the user executing the progam and inserts it in the tar command.

The output of the print statements are this. The variables print right, but I get the bash error.
Code:
list = /home/tangle/Shared
user = tangle
bk_cmd = tar cvf
path = /home/tangle/
run_bk = tar cvf /home/tangle/tangle.tar /home/tangle/Shared
Here is the earlier script.
Code:
import os
import commands
import datetime

def cls():
    # Define the clear windows command
    clear = commands.getoutput('clear')
    print clear

cls()
# Print introduction
print "This program will make a backup of a list of files in file_list.txt file."
print
print "Do you want to continue? ",
con = raw_input("[y/n]: ")
if con == "y":
    print "Lets get started then"
    cls()    
    print "Where do you want the backup file to be writtten?"
    path_name = raw_input("Path to Write to: ")
    cls()    
    print "What would you like to name the backup file?"
    bk_name = raw_input("Backup Name: ")
    cls()    
    print "What is the name of the file or directory you would like to backup?"
    ob_name = raw_input("Backup Data: ")
    cls()    
    print "Do you want to compress the file? ",
    compress = raw_input("[y/n]: ")
    bk_cmd = "tar cvf "
    bkz_cmd = "tar cvfz "
    if compress == "n":
        run_bk = bk_cmd + path_name + "/" + bk_name + ".tar" + " " + ob_name
    if compress == "y":
        run_bk = bkz_cmd + path_name + "/" + bk_name + ".tar.gz" + " " + ob_name
    cls()    
    print "Checking to see if the directory exists."
    file_check = os.path.isfile(ob_name)
    dir_check = os.path.exists(ob_name)
    if file_check == 1:
        output = commands.getoutput(run_bk)
        print output
        cls()        
    elif dir_check == 1:
        output = commands.getoutput(run_bk)
        print output
        cls()        
    elif file_check == 0 and dir_check == 0:
        cls()
        print "ERROR: The object does not exsist!"
        print
        print
else:
    cls()
    print "Goodbye"
    print
 
Old 02-04-2006, 02:14 AM   #2
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86; Gentoo PPC; Gentoo Sparc64; FreeBSD; OS X; Solaris
Posts: 3,731
Blog Entries: 4

Rep: Reputation: 68
Why commands.getoutput()?

Just use:
Code:
os.system(run_bk)
 
Old 02-04-2006, 10:26 AM   #3
tangle
Senior Member
 
Registered: Apr 2002
Location: Smithville, TN
Distribution: Slackware
Posts: 1,744

Original Poster
Rep: Reputation: 70
I tried to use os.system earlier in the day and it didn't work (works now in both scripts, must of did something wrong). I read somethere that the commands.getoutput() could be used and that it was more secure than os.system. I just tried the os.system and it worked, thanks.
 
Old 02-04-2006, 12:55 PM   #4
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86; Gentoo PPC; Gentoo Sparc64; FreeBSD; OS X; Solaris
Posts: 3,731
Blog Entries: 4

Rep: Reputation: 68
Well, I'm not sure commands.getoutput() is inherently more secure. Both commands are only as secure as the input you give them, so of course in your final version you will want to scrub/check any user input carefully to make sure no funny business is going on.

Seems to me commands.getoutput() is more appropriate when you specifically want to do something with the output rather than just run the command and be done with it.
 
Old 02-04-2006, 03:22 PM   #5
tangle
Senior Member
 
Registered: Apr 2002
Location: Smithville, TN
Distribution: Slackware
Posts: 1,744

Original Poster
Rep: Reputation: 70
I wasn't sure either why commands.getoutput() is more secure than os.system(). As you said, os.system() make more sense to run a command and forget about it. Thanks for the reply.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script- command on exit PDock Programming 6 12-23-2005 07:03 PM
Bash backup script failing using tar and --newer option saldkj Programming 3 03-12-2005 12:03 PM
BASH - simple script issue - syntax? tw1ggy5 Programming 11 05-21-2004 09:09 AM
Bash script (untar *.tar files and then cd to the untared dir) k0ljat Linux - Newbie 2 12-30-2003 05:57 AM
help with basic syntax in bash script Supp0rtLinux Linux - Software 4 03-27-2003 06:57 PM


All times are GMT -5. The time now is 03:30 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration