Sponsored Content
Top Forums Shell Programming and Scripting Script to delete files with an input for directories and an input for path/file Post 302664055 by Chubler_XL on Thursday 28th of June 2012 07:35:17 PM
Old 06-28-2012
This should get you started you might want to implement more actions (like prompt) and perhaps control which actions occur with command line args check out getopts

Code:
if [ $# -ne 2 ]
then
   echo "usage: $0 list_file dir_file"
   exit 1
fi
LIST_FILE=$1
DIR_FILE=$2
if [ ! -f "$LIST_FILE" ]
then
   echo "unable to open list_file: $LIST_FILE"
   exit 2
fi
if [ ! -f "$DIR_FILE" ]
then
   echo "unable to open dir_file: $DIR_FILE"
   exit 3
fi
found=0
for action in list count del
do
    for del_dir in $(cat $DIR_FILE)
    do
        for del_path in $(cat $LIST_FILE)
        do
            [ -f ${del_dir}/${del_path} ] && case $action in
               count) let found=found+1 ;;
               list) echo "  ${del_dir}/${del_path}" ;;
               del) rm ${del_dir}/${del_path} ;;
            esac
        done
    done
    if [ $action = "count" ]
    then
        [ $found -eq 0 ] && echo "No files found to delete" && exit 5
        printf "OK to delete %d file(s) (Y/N)? " $found
        read ans
        [ "$ans" != "Y" -a "$ans" != "y" ] && exit 0
    fi
done

 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File path with space as external input to the program

Hello I am getting error when the file (Folder or Application) path having space is given as external input to the shell program. It works fine for the files which has no spaces in the file name Thans, (5 Replies)
Discussion started by: keshav.murthy@r
5 Replies

2. Shell Programming and Scripting

a remove script taken in input a file which contain a list of directories

Hi, I'm looking to delete some files from directories. I've just put in a file the location of these files. e.g: in file supprs.txt there is: /usr/host/t1.txt /etc/dev/u1.java /home/new/files/view.c Is it possible to take this file "supprs.txt" as a parameter in a shell command ? (2 Replies)
Discussion started by: yeclota
2 Replies

3. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

4. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies

5. Shell Programming and Scripting

Need script to take input from file, match on it in file 2 and input data

All, I am trying to figure out a script to run in windows that will allow me to match on First column in file1 to 8th Column in File2 then Insert file1 column2 to file2 column4 then create a new file. File1: 12345 Sam 12346 Bob 12347 Bill File2:... (1 Reply)
Discussion started by: darkoth
1 Replies

6. UNIX for Dummies Questions & Answers

Bash script to delete file input on command line

1) I wrote a script and gave the desired permissions using "chmod 755 scriptname". Now if i edit the script file, why do i need to set the permission again? Didn't i set the permission attribute.. or if i edit the file, does the inode number of file changes? 2) I am running my unix on a server... (1 Reply)
Discussion started by: animesharma
1 Replies

7. Shell Programming and Scripting

Need to delete large set of files (i.e) close to 100K from a directory based on the input file

Hi all, I need a script to delete a large set of files from a directory under / based on an input file and want to redirect errors into separate file. I have already prepared a list of files in the input file. Kndly help me. Thanks, Prash (36 Replies)
Discussion started by: prash358
36 Replies

8. Shell Programming and Scripting

Find Files with a input in directories

So i have directories that store logs, 1 directorie for each day, with the name like this : 2012_07_01/ 2012_07_02/ and for each directorie we have the logs, inside them lives the logs for that day, and every log have this name pattern : ... (2 Replies)
Discussion started by: drd0spt
2 Replies

9. Shell Programming and Scripting

Script to delete files older than x days and also taking an input for multiple paths

Hi , I am a newbie!!! I want to develop a script for deleting files older than x days from multiple paths. Now I could reach upto this piece of code which deletes files older than x days from a particular path. How do I enhance it to have an input from a .txt file or a .dat file? For eg:... (12 Replies)
Discussion started by: jhilmil
12 Replies

10. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies
bosh(1) 						      General Commands Manual							   bosh(1)

NAME
bosh - Browsable Output SHell SYNOPSIS
bosh [OPTIONS] [CONFIGURATION] [CONFIGURATION OPTIONS] DESCRIPTION
bosh takes the output of a program or script and provides a curses interface to browse that output. A particular line of that output can be selected and actions can be defined and executed and make use of the that selected line. USAGE
CONFIGURATION is the name of a bosh configuration file (see below), in which case that is loaded. If CONFIGURATION is absent, and bosh is invoked on the end of a pipe, it will read from stdin. Bosh now supports passing arguments to the CONFIGURATION. The arguments will be available in the standard way ($1...$9,$*,$@,etc). Bosh can be invoked as above, or as "interpreter", meaning it can invoked from a shebang (#!) line at the top of a script. This script would just be a bosh configuration file. See bops as an example, which should have come with bosh. OPTIONS
-h / --help show help and exit -v / --version show version and exit --autorefresh=N Automatically re-run command every N seconds. --cursorsize=N Set the cursor to N lines high. --cursormovement=N Set how much the cursor moves one an up/down keypress. --header=[N] Prevent the cursor from entering the first N rows of the output. --multilineseperator=STRING When an action is invoked and the cursor is multi-line, the lines selected will be concatenated together. With this setting a sepa- rating string can be specified to be inserted between the lines. --preaction=COMMANDS A command or commands that will be run on the invocation of all actions, before the action is run. This allows code that is a com- mon for the actions to be only defined once. Preactions are simply prefixed onto the action when the action is invoked. This means you will need to include a separating character (eg ;) at the end of preaction. --refresh=[0,1] A value of 1 means that bosh will re-run the command after an action is performed. --uservars=N Set the number of user variables ( of the form $BOSHVARx ) available. See the USER VARIABLES section below. CONFIGURATION FILES
Bosh configs are fairly simple. Firstly you need a line which tells bosh the actual program to execute to show it it's buffer - command=ps x It could also be a chain of commands (bash) - command=for i in *; do echo $i; done Or it can spread it over multiple lines for readability with a (must be at the end of line!) - command=for i in * do echo $i done Or now even better, bosh supports blocks delimited by {{ and }} - command{{ for i in * do echo $i done }} These can be used with all options and actions. Command line arguments given to bosh after the COMMAND parameter are available and can be used as follows - command=ps $* This would allow the user to specify the format of ps when invoking bosh. Commands can also set BOSHERR. When execution of the command finishes, bosh will exit and display the value of BOSHERR if it has been set. command=if [ -z "$1" ] then BOSHERR="usage: $BOSHCONF [SECTION] NAME" return 1 fi man $* This will mean bosh exits immediately if no arguments are passed on the command line. Note the use of return rather than exit. After the command option, you can specify any of the options specified above in the OPTIONS section, but without the -- prefix - header=4 refresh=1 ACTIONS
Basic actions are defined as - KEY=command eg: k=kill $(echo $BOSH | cut -f1 -d' ') 9=kill -9 $(echo $BOSH | cut -f1 -d' ') Or, using the preaction setting (see above) - preaction=PID=$(echo $BOSH | cut -f1 -d' '); k=kill $PID 9=kill -9 $PID The keys available are a-z,0-9 and enter. Bosh keys are not case sensitive, so A= is the same as a=. $BOSH is an environment variable containing the currently selected line(s) in bosh. It is set when the action key is invoked. This is how information is passed to the actions. In the example above, the PID is extracted from the currently selected line of the ps output using cut, which can then be passed to the kill command. ACTIONS WITH OUTPUT For basic actions such as kill, which has no output to stdout, the above definition is sufficient. However, bosh can now intercept the out- put of actions and place that in the bosh window. These are defined as follows - KEY=[.]command Or, eg: l=[.]/usr/sbin/lsof -p $PID Assuming the preaction is used above, this action will use lsof to show in bosh a list of files that process $PID has open. In this situa- tion, the output of the original command is lost, and replaced with the output of the action. Alternatively an action can be defined - KEY=[>]command In this situation, bosh is like a web browser, in that this output (lsof) will not override the current buffer, but create a new buffer - You can get then move back and forward through these buffers with the left and right arrow keys. At this stage, actions are only available in the original buffer. The other possibility is that an action may be required that has output that isn't to be shown in the bosh window, such as other curses- based applications. So the following syntax will make bosh end curses mode when this action is invoked. KEY=[!]command eg: If the bosh window contained a list of files, an action like this could be used to load that file in pico. e=[!]pico $BOSH ACTION PARAMETERS Actions can now have a prompt for user input before performing the action. The value is available to the action using the $BOSHPARAM vari- able. eg: Using the ps example above, with PID preaction - s=[!:signal] kill -s $BOSHPARAM $PID When this action is called, bosh will ask for user input with the prompt signal: . Once this has been entered, the action will run. BOSH* VARIABLES: In addition to $BOSH , $BOSHPARAM and $BOSHERR (all explained above), the following variables available to actions - $BOSHPID Process ID of bosh itself $BOSHPPID Parent process ID of bosh (eg: the shell you ran bosh from) USER VARIABLES User variables are variables to be set and used by commands and actions. They are of the form $BOSHVARx. When the command or action is run and sets a user variable, bosh will store the contents when that command or action has finished. This allows the values to be used by sub- sequent actions. To make use of these, you must first set the uservars to the number you need (eg: uservars=1 will give you BOSHVAR1, user- vars=10 will give you BOSHVAR1 thru BOSHVAR10). SHELLS
Currently bosh only supports bash as the shell that it spawns for executing the commands and actions. Support for other shells and lan- guages will hopefully be included in the future. EXAMPLE CONFIGURATION
: Included with bosh should be a simple configuration named bops. It uses ps as the main command, and allows you to kill the selected process or view its open files (using lsof). This is where the above examples are taken from. The original inspiration for bosh was being able to kill processes easily in this manner. To run bops, type - $ ./bops This invokes bosh through the shebang at the top (assuming the path is set correctly). Or to run it the traditional way - $ ./bosh ./bops KEYS
UP/DOWN cursor up/down LEFT/RIGHT buffer forward/back ^L refresh screen ^O run new command ^P pipe buffer through a command, with the output of that pipe will become the buffer ^R refresh program output (re-run the command) ^V show the current configuration ^W search ^N repeat search ^X exit F3 same as ^W F4 same as ^N F5 same as ^R F6 reload configuration F12 same as ^L | same as ^P STATUS BAR
The status bar contains some further information about the current configuration. It shows with exit=num the last exit value of a command run in bosh. Furthermore a R indicates that bosh is running with refresh option activated. In the status bar there will be a countdown shown if the autorefresh option is set. AUTHOR
Alex Sisson (alexsisson@gmail.com) HOMEPAGE
Check for updates at http://bosh.sourceforge.net bosh-0.6 18-MAR-2008 bosh(1)
All times are GMT -4. The time now is 01:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy