Tag Archives: DIR

Expect Script – Accepting the key

By app1mvf

Hello, I have done some research and I have found various example of people accepting the key dynamicly but I cant find an example that works for the expect script I wrote (more like copied someone elses example of a except script)…

In the below sample of code I am loging into a remote server and checking for the existance of directories… The isse I am having is if the remote server is rebooted then the key would need to be accepted…. I am fully aware that I can bypass the accepting of the key by using “-o StrictHostKeyChecking=no” in conjunction with my ssh command but I would like to add logic whereas I would like the expect script to handle the accepting of the keys dynamicly rather than bypassing…. Using my sample below could someone help me to alter the script below to accept the key when prompted… I would greatly appreaciate all help. Thank you.

Code:

#!/bin/bash

rm -f /tmp/expect.result

Directory_List='/tmp/DIR.Drv'

RServer_Task() {

# build the expect script in bash

expect_sh=$(expect -c "
spawn ssh user@192.168.1.172
expect "password:"
send "ABCDEFGr"
expect "#"
send "ls -d $Recr"
expect "#"
send "exitr"
expect "#"
")

# run the expect script
echo "$expect_sh"
}

while read $Rec
do
RServer_Task > /tmp/expect.output
grep 'No such file or directory' /tmp/expect.output >> /tmp/expect.result
done<${Directory_List}


From: http://www.unix.com/shell-programming-scripting/221067-expect-script-accepting-key.html

How to replace all ip addresses in all files in directory in ksh?

By sanzee007

Hi All,
I have a directory containing a lot of files and I want to search all files which contains IP addresses pattern and want to replace those ip addresses with equal number of, say, some character, ‘x’. So after replacement the file should not contain any IP address. Like
10.122.53.5 -> xx.xxx.xx.x
12.176.254.24 -> xx.xxx.xxx.xx
the replacement should not change anything other than those IP address numbers, retaining all white spaces, newlines etc. intact.

for fname in $(find “$DIR” -type f -print)
do
#for each file replace the IP numbers here in the file
done

How can I achieve this using commands like grep/sed/awk/tr or may be combining use of all these commands?

Please do care about the different shells working behavior like something may work in bash, but may not work in korn shell. I am using ksh for this.

Thanks in advance for the replies.
sanzee

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

Bash For Loop Help

By zenyoul

This is a short program I wrote to search through a directory and move files containing the keyword to a folder.

Code:

#!/bin/bash

echo 'What is the directory?'
read DIR
echo -e 'n'

echo 'What is the keyword?'
read KEY

echo -e 'n'

cd $DIR
rmdir 'relevant_files'
mkdir 'relevant_files'

for entry in 'grep -ilr $KEY $DIR'; do
echo $entry
mv $entry 'relevant_files'
done


The main problem I’m having with is the for loop.

Code:

for entry in 'grep -il $KEY $DIR'; do
echo $entry
mv $entry 'relevant_files'
done


My question is, how would I make the for loop entries equal to each file grep comes up with in the directory?

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

Remove the file except with particular extension

By vikatakavi

Hi all

i am new for the shell scripting can any one help me with my requirments . i want to delete file older than 21 days everything works fine but in that dir i got the files with should not be deleted with particular extension like (.info):confused:here is the script i wrote .can anyone help me how to twick the below script

for files in $DIR
do
echo “Looking for Files in $MAPR_DIR”
find $DIR/* -mtime +21 -exec rm {} ;
echo “Deleted found files”
done

…read more

Source: FULL ARTICLE at The UNIX and Linux Forums

Perl : printing the files in the exact order as it is..

By scriptscript

Hi folks,

I am trying to open directory and then print the files in that directory in PERL.
I have used the below code.

Code:

opendir(DIR,$destination) or die ("Cannot open directory $destination");
my @bwfiles = readdir(DIR);
print @bwfiles;


I am able to retrieve all the files the files are not printed according to the timestamp.

In directory files are listed as below..

Code:

-rw-r--r-- 1 johng users 10471630 2013-03-19 12:56 file1.txt
-rw-r--r-- 1 johng users 45496 2013-03-19 13:37 file2.txt
-rw-r--r-- 1 johng users 45512 2013-03-24 13:42 file3.txt
-rw-r--r-- 1 johng users 0 2013-03-28 06:17 file4.txt
-rw-r--r-- 1 johng users 0 2013-03-28 06:18 file5.txt


But while printing the files..

Code:

file5.txt
file3.txt
file4.txt
file1.txt
file2.txt


But I expected is

Code:

file1.txt
file2.txt
file3.txt
file4.txt
file5.txt


How to print them in exact order in the directory..

Could anyone please help me on this ?

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Help need with remove commnad

By pinnacle

Hi,
Help need with remove commnad

I am using shell script where in I delete old files using the following commands:

Code:


rm ${DIR}/${pattern}*


We source the value of ${DIR} and ${pattern} from a config file. In some instances when the config file was missing the above parameters or say sourcing did not happen.
Then ${DIR} and ${pattern} becomes blank and the remove statement was executed as

Code:


rm /*


But luckily no damage was done.
I read the documentation and not really sure which one is best for me.

Quote:

${parameter:-word}
If parameter is set and is non-null then substitute its value;
otherwise substitute word.
${parameter:=word}
If parameter is not set or is null then set it to word; the
value of the parameter is then substituted. Positional parame-
ters may not be assigned to in this way.
${parameter:?word}
If parameter is set and is non-null then substitute its value;
otherwise, print word and exit from the shell (if not interac-
tive). If word is omitted then a standard message is printed.


Help is appreciated. I want to use the safest approach as I am afraid to use rm commands.

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Generating a Config File

By mora

HI,

I want to append some configuration statements to the existing file in unix to it’s 3 line , 7th line and 28 line.

There is a file generated by our system job, to this I need to add some lines as mentioned below.

DIR=/usr/CDR – line 3
Source=/usr/src – line 7
Target=/usr/tgt – line 28

Please suggest how can I add these about to the specific file generated by other process.

Regards,
Mora

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Strange Problem in case statement while shift

By zaq1xsw2

Hi,

Here is my code as below:

test.ksh:
=======

Code:


#!/bin/ksh
option="${1}"
while [ $# -gt 0 ]
do
case $1 in
-f) FILE="${2}"
echo "File name is $FILE"
;;
-d) DIR="${2}"
echo "Dir name is $DIR"
;;
-*)
echo "`basename ${0}`:usage: [-f file] | [-d directory]"
exit 1 # Command to come out of the program with status 1
;;
*) ;;
esac
shift 1
done


While i am running the above script:

Code:


[kanix@voodoo mydir]$ ./test.ksh -f "abc.txt"
File name is abc.txt


The above output is as expected.. But when i am running as below:

Code:


[kanix@voodoo mydir]$ ./test.ksh -f "-abc.txt"
File name is -abc.txt
test.ksh:usage: [-f file] | [-d directory]


the usage code is getting displayed.. which is not expected..

But again when i am providing a space between the quote and hyphen it is again showing the expected result.

Code:


[kanix@voodoo mydir]$ ./test.ksh -f " -abc.txt"
File name is -abc.txt


Please advise why this is happening and how to fix this.. i need to run the code as : ./test.ksh -f “-abc.txt”

…read more
Source: FULL ARTICLE at The UNIX and Linux Forums

Not able to understand IFS

By scriptor

Hi ,

i am in my initial learning phase of unix. i was going thru the function part.
below is the example which was there but i am not able to understand logic and the use of IFS(internal field separator)

Code:

lspath() {
OLDIFS="$IFS"
IFS=:
for DIR in $PATH ; do echo $DIR ; done
IFS="$OLDIFS"
}


the output is below. here i am failed to understand how the o/p is coming like below
and logic behind the IFS.

Code:

/sbin
/bin
/usr/bin
/usr/sbin
/opt/bin
/usr/ucb
/usr/ccs/bin
/usr/openwin/bin


regards,
scriptor

Source: FULL ARTICLE at The UNIX and Linux Forums

Trying to test for both upper and lower case directories

I am trying to get a script to print out whether a directory is lowercase uppercase or both. This is what I’ve got so far:

echo -e read “enter name”
read server
for DIR in $(find /tmp/$server -type d -prune | sed ‘s/.///g’);do if expr match “$server” “[a-zA-Z]*$” > /dev/null; then echo “$server – Both”; elif expr match “$server” “[[:upper:]]*$”; then echo “$server – Uppercase”; elif expr match “$server” “[[:lower:]]*$” > /dev/null; then echo “$server -there are lower”;fi;doneNow this will let me know whether either an upper or lowercase directory is present for the servername and also if both are present, like this

/tmp/server -lowercase present
/tmp/SERVER – uppercase present.
if both are present, it prints both

However, if I type in a lowercase name and there is only a lowercase directory, it still prints “both.” What am I doing wrong? I think “[a-zA-Z]* is bad because it matches incorrectly. I think I need something like “[[:upper:]]*$ && [[lower:]]” but this does not work.

Any help would be greatly appreciated!

Program

my this program is working when the argument is not provided why its not working when i m providing argument?

code :
#include
#include
using namespace std;
int main(int agrc ,char *val[])
{
DIR*dir;
struct dirent * abcd;
if(!val[1])
{
/*cout d_name[0] != ‘.’ )
printf (“[%s]n”, abcd->d_name);

}
closedir(dir);

exit(0);
}
dir = opendir(val[1]);
if(dir !=NULL)
{
while ((abcd=readdir (dir))!=NULL)
{
if( abcd->d_name[0] != ‘.’ )
printf (“[%s]n”, abcd->d_name);
}
closedir(dir);
return 0;
}
}
}
Source: The UNIX and Linux Forums