Sponsored Content
Top Forums Shell Programming and Scripting creating a simple archiving script Post 302149160 by lensmen on Wednesday 5th of December 2007 06:18:13 AM
Old 12-05-2007
creating a simple archiving script

Im trying to create a script to archive specified directories into a specified tarball backup file. This is what i want the input to look like
ex. save -i '/bin/b*' -i '/bin/ls' -o backup

this is what i have
#!/bin/bash
#save - backup file script
unset myInput
unset myOutput
while getopts "i: o:" myOpt
do
if [ $myOpt == "i" ]
then
myInput=$myInput" "$OPTARG
elif [ $myOpt == "o" ]
then
myOutput=$OPTARG
fi
done
echo "[Backing up $myInput to $myOutput]"
if test -f $myOutput
then
echo "$myOutput already exists: must rename"
fi
echo "Input file list: "
for file in $myInput
do
ls -laR "$file"
done
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

creating an archiving script

Hi all. New to unix and need a little help. I am trying to create a script to archive files or directories in to a tarball. I've played a little with scripts but dont understand how to do options. What i want to be able to do is give the command any number of inputs and an output. ex.... (2 Replies)
Discussion started by: jinxe
2 Replies

2. Shell Programming and Scripting

Creating simple shell program

Hi, I'm new to UNIX shell programming... can anyone help in doing the following : 1) create a text file named "Model File" having following columns : Name Number Physics Chemistry 2) prompt user to n rows enter the name, number, physics ,chemistry 3) display the entire columns and rows... (1 Reply)
Discussion started by: Mayuri P R
1 Replies

3. UNIX for Dummies Questions & Answers

Help with creating a simple program!!

i am new to shell scripting!! i am making this program in bourne shell, that asks the user to input "Hello (their name)" or "question (their name)", any other input, "ERROR" will be outputted. if they input "Hello (name)", i want to out saying Hello (name) but if they input "question (name)", i... (4 Replies)
Discussion started by: bshell_1214
4 Replies

4. Shell Programming and Scripting

how to execute ksh simple shell script without creating .sh file

please help me to execute a simple shell script like for i in `ls echo $i done . i dont want to create a new sh file to execute it. Can i just type and execute it ? because I always this kind of simple for loops . Please help . Thanks (7 Replies)
Discussion started by: Sooraj_Linux
7 Replies

5. Shell Programming and Scripting

Simple archiving

Hi, I am a UNIX novice and was trying to write a bash script that would read two parameters, $1 would be the archive file path where the archiving path is passed, so the archiving script will write to this file path. The second parameter will be the directory path to the library which will... (0 Replies)
Discussion started by: pernuntium
0 Replies

6. Shell Programming and Scripting

Archiving and moving files into directories, creating directories, etc.

how can i move "dataName".sql.gz into a folder called 'database' and then move "$fileName".tar.gz * .htaccess into a folder called 'www' with the entire gzipped file being "$fileName".tar.gz? Is this doable or overly complex. so mydemo--2015-03-23-1500.tar.gz > database -... (5 Replies)
Discussion started by: wyclef
5 Replies

7. Shell Programming and Scripting

Archiving files using shell script

Dear Team, I am looking for transferring files to and from the local and remote servers using SFTP commands. Currently the script is using the mget and mput commands to do the copying of the files. While I am trying to move the files from local to remote server, I would also like to archive... (21 Replies)
Discussion started by: Rads
21 Replies

8. UNIX for Beginners Questions & Answers

Creating a simple ID Script

Hello everybody, :wall:I am new to Linux and I want to create a simple script on Ubuntu that will allow to make database with a few perimeters. create file and name it Database and complete it with any information id firstname lastname phone 0 1 2 3 4 ... (4 Replies)
Discussion started by: kkishore4580
4 Replies

9. Homework & Coursework Questions

Help with creating a simple shell script

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Write a shell script that accepts two arguments. The two arguments are the name of 2 files. • If the arguments... (3 Replies)
Discussion started by: Scripter12345
3 Replies
bgexec(n)						       BLT Built-In Commands							 bgexec(n)

__________________________________________________________________________________________________________________________________________________

NAME
bgexec - Run programs in the background while handling Tk events. SYNOPSIS
blt::bgexec varName ?option value?... program ?arg?... _________________________________________________________________ DESCRIPTION
The bgexec command executes programs in the background, allowing Tk to handle events. A global Tcl variable varName is set when the pro- gram has completed. INTRODUCTION
Tcl's exec command is very useful for gathering information from the operating system. It runs a program and returns the output as its result. This works well for Tcl-only applications. But for Tk applications, a problem occurs when the program takes time to process. Let's say we want the get the disk usage of a directory. We'll use the Unix program du to get the summary. set out [exec du -s $dir] puts "Disk usage for $dir is $out" While du is running, scrollbars won't respond. None of the Tk widgets will be redrawn properly. The send command won't work. And the worst part is that the application appears hung up or dead. The problem is that while exec is waiting for du to finish, Tk is not able to handle X events. The bgexec command performs the same functions as exec, but also allows Tk to handle events. You can execute a long-running program and the Tk widgets will behave normally. When the program finishes, its output and the exit status are written to Tcl variables. This makes it easy to monitor and save the output of a program. EXAMPLE
Here is the disk usage example again, this time using bgexec. The syntax to invoke "du" is exactly the same as the previous example, when we used exec. global myStatus myOutput blt::bgexec myStatus -output myOutput du -s $dir puts "Disk usage for $dir is $myOutput" Two global variables, myStatus and myOutput, will be set by bgexec when du has completed. MyStatus will contain the program's exit status. MyOutput, specified by the -output option, will store the output of the program. You can also terminate the program by setting the variable myStatus. If myStatus is set before du has completed, the process is killed. Under Unix, this is done sending by a configurable signal (by default it's SIGKILL). Under Win32, this is done by calling TerminateProcess. It makes no difference what myStatus is set to. set myStatus {} There are several bgexec options to collect different types of information. global myStatus myOutput myErrs blt::bgexec myStatus -output myOutput -error myErrs du -s $dir The -error option is similar to -output. It sets a global variable when the program completes. The variable will contain any data written to stderr by the program. The -output and -error variables are set only after the program completes. But if the program takes a long time, to run you may want to receive its partial output. You can gather data as it becomes available using the -onoutput option. It specifies a Tcl command prefix. Whenever new data is available, this command is executed, with the data appended as an argument to the command. proc GetInfo { data } { puts $data } blt::bgexec myStatus -onoutput GetInfo du -s $dir When output is available, the procedure GetInfo is called. The -onerror option performs a similar function for the stderr data stream. Like exec, bgexec returns an error if the exit code of the program is not zero. If you think you may get a non-zero exit code, you might want to invoke bgexec from within a catch. catch { blt::bgexec myStatus -output myOutput du -s $dir } By default, bgexec will wait for the program to finish. But you can detach the program making ampersand (&) the last argument on the com- mand line. global myStatus myOutput blt::bgexec myStatus -output myOutput du -s $dir & Bgexec will return immediately and its result will be a list of the spawned process ids. If at some point you need to wait for the program to finish up, you can use tkwait. When the program finishes, the variable myStatus will be written to, breaking out the tkwait command. global myStatus myOutput blt::bgexec myStatus -output myOutput du -s $dir & ... tkwait variable myStatus SYNTAX
The bgexec command takes the following form: blt::bgexec varName ?option value?... program ?arg?... VarName is the name of a global variable which is set when program has finished executing. The exit status of will be stored in varName. The exit status is a list of a status token, the process-id of the program, the exit code, and a status message. You can also prematurely terminate the program by setting varName. Under Unix, the program will be sent a signal to terminate it (by default the signal is a SIGKILL; see the -killsignal option). Program is the name of the program to be executed and args are any extra arguments for program. The syntax of program and args is the same as the exec command. So you can redirect I/O, execute pipelines, etc. (see the exec manual for further information) just like exec. If the last argument is an ampersand (&), the program will be run detached, and bgexec will return immediately. VarName will still be set with the return status when program completes. OPTIONS
Option refers to the switch name always beginning with a dash (-). Value is the value of the option. Option-value pairs are terminated either by the program name, or double dashes (--). The following options are available for bgexec: -decodeerror encodingName Specifies the encoding of the stderr channel. This affects only data returned to the Tcl interpreter. No translation is done on file redirection. For example if data is to be converted from Unicode for use in Tcl, you would use the "unicode" encoding. The default is that no translation is performed. -decodeoutput encodingName Specifies the encoding of the stdout channels. This affects only data returned to the Tcl interpreter. No translation is done on file redirection. For example if data is to be converted from Unicode for use in Tcl, you would use the "unicode" encoding. The default is that no translation is performed. -error varName Specifies that a global variable varName is to be set with the contents of stderr after the program has completed. -keepnewline boolean Specifies that a trailing newline should be retained in the output. If boolean is true, the trailing newline is truncated from the output of the -onoutput and -output variables. The default value is true. -killsignal signal Specifies the signal to be sent to the program when terminating. This is available only under Unix. Signal can either be a number (typically 1-32) or a mnemonic (such as SIGINT). If signal is the empty string, then no signal is sent. The default signal is 9 (SIGKILL). -lasterror varName Specifies a variable varName that is updated whenever data becomes available from standard error of the program. VarName is a global variable. Unlike the -error option, data is available as soon as it arrives. -lastoutput varName Specifies a variable varName that is updated whenever data becomes available from standard output of the program. VarName is a global variable. Unlike the -output option, data is available as soon as it arrives. -linebuffered boolean Specifies that updates should be made on a line-by-line basis. Normally when new data is available bgexec will set the variable (-lastoutput and -lasterror options) or invoke the command (-onoutput and -onerror options) delivering all the new data currently available. If boolean is true, only one line at a time will be delivered. This can be useful when you want to process the output on a line-by-line basis. The default value is false. -output varName Specifies that a global variable varName is to be set with the output of the program, once it has completed. If this option is not set, no output will be accumulated. -onerror command Specifies the start of a Tcl command that will be executed whenever new data is available from standard error. The data is appended to the command as an extra argument before it is executed. -onoutput command Specifies the start of a Tcl command that will be executed whenever new data is available from standard output. The data is appended to the command as an extra argument before it is executed. -update varName Deprecated. This option is replaced by -lasterror. -- This marks the end of the options. The following argument will be considered the name of a program even if it starts with a dash (-). PREEMPTION
Because bgexec allows Tk to handle events while a program is running, it's possible for an application to preempt itself with further user- interactions. Let's say your application has a button that runs the disk usage example. And while the du program is running, the user accidently presses the button again. A second bgexec program will preempt the first. What this means is that the first program can not finish until the second program has completed. Care must be taken to prevent an application from preempting itself by blocking further user-interactions (such as button clicks). The BLT busy command is very useful for just these situations. See the busy manual for details. DIFFERENCES WITH FILEEVENT
Since Tk 4.0, a subset of bgexec can be also achieved using the fileevent command. The steps for running a program in the background are: Execute the program with the open command (using the "|" syntax) and save the file handle. global fileId set fileId [open "|du -s $dir" r] Next register a Tcl code snippet with fileevent to be run whenever output is available on the file handle. The code snippet will read from the file handle and save the output in a variable. fileevent fileId readable { if { [gets $fileId line] < 0 } { close $fileId set output $temp unset fileId temp } else { append temp $line } } The biggest advantage of bgexec is that, unlike fileevent, it requires no additional Tcl code to run a program. It's simpler and less error prone. You don't have to worry about non-blocking I/O. It's handled transparently for you. Bgexec runs programs that fileevent can not. Fileevent assumes that the when stdout is closed the program has completed. But some pro- grams, like the Unix compress program, reopen stdout, fooling fileevent into thinking the program has terminated. In the example above, we assume that the program will write and flush its output line-by-line. However running another program, your application may block in the gets command reading a partial line. Bgexec lets you get back the exit status of the program. It also allows you to collect data from both stdout and stderr simultaneously. Finally, since data collection is handled in C code, bgexec is faster. You get back to the Tk event loop more quickly, making your applica- tion seem more responsive. SEE ALSO
busy, exec, tkwait KEYWORDS
exec, background, busy BLT
2.4 bgexec(n)
All times are GMT -4. The time now is 06:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy