How to redirect the output of a command inside ftp block?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to redirect the output of a command inside ftp block?
# 1  
Old 07-26-2013
How to redirect the output of a command inside ftp block?

hi,

i am using ftp to get files from remote server. inside the ftp i want to us ls -ltr command and send the output of it to a file.

Code:
ftp -n remote_server <<_FTP
    quote USER username
    quote PASS password
    prompt noprompt
    pwd
    ls -ltr
    get s1.txt
    bye
_FTP

i want to get the output of only ls -ltr command to be stored in a file and not the other commands like pwd and get.

is it possible to redirect the output of only ls -ltr command into a file.
# 2  
Old 07-26-2013
You can try an expect script for interactive applications like ftp, its possible to capture the output of previous command and direct it to a file.


-Raja
# 3  
Old 07-26-2013
please give me an example using expect and capturing the output of particular command in a file.
# 4  
Old 07-26-2013
This is a basic expect script from which you can start

Code:
#!/usr/bin/expect
set timeout 10
spawn ftp -n remote-server
expect "ftp> "
send "quote USER username\r"
expect "ftp> "
send "quote PASS password\r"
expect "ftp> "
send "prompt noprompt\r"
expect "ftp> "
send "pwd\r"
expect "ftp> "
send "ls -ltr\r"
expect "ftp> "
# create the file to write the last command output
set filename "ls-output.txt"
set fd [open $filename "w"]
puts $fd $expect_out(buffer)
close $fd
send "get s1.txt\r"
expect "ftp> "
send "bye\r"
exit

# 5  
Old 07-26-2013
If this is too tricky, you could try this:-
Code:
ftp -n remote_server <<_FTP > $LOGFILE
    quote USER username
    quote PASS password
    prompt noprompt
    pwd
    !echo "MARKED START\n"
    ls -ltr
    !echo "\nMARKED END"
    get s1.txt
    bye
_FTP

  • Note, you may need to use !print .... to get the new-line, depending on your OS.
You then will have a section of the output you can retrieve between the marked start & end. If you have the -p flag on grep, this becomes relatively trivial:-
Code:
egrep -vp "MARKED START|MARKED END" $LOGFILE

The -p flag matches a paragraph, so all the output up to the blank line after the "MARKED START" and all the text after the blank before the "MARKED END" will be excluded by the grep -v


Does this help?



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to redirect output of a command to a variable during ftp mode?

Hi, How can I redirect the output of a following command to a variable. ftp myservername ftp> user (username) guptaji 331 Password required for guptaji. Password: ftp> size /home/salil/abc.dat ftp> a='size /home/salil/abc.dat' ?Invalid command I want to redirect value of size to... (1 Reply)
Discussion started by: Salil Gupta
1 Replies

2. Shell Programming and Scripting

How to use chmod command inside ftp block?

hi, i want to use chmod command inside ftp. so that what ever files are transfered to the local server will hav 664 permission. if i use chmod inside ftp , the file permissions gets changed in the remote server and when the file is transffered to local server using get command, it does not... (8 Replies)
Discussion started by: Little
8 Replies

3. AIX

Not able to redirect output of command

Hi All,. We are using AIX as the OS to host the Oracle ERP. We have a command FNDLOAD which is used to load setups. When this command is run, it outputs names of log files and any errors to the screen. I am trying to redirect this output to a file because we have large number of these... (4 Replies)
Discussion started by: mansmaan
4 Replies

4. Shell Programming and Scripting

FTP Command inside If condition

Hi All, I am facing a alien problem in unix shell script. i have a requirement where i have to check first whether a particular file has been created or not if that file has been created then that file needs to be ftped to a particular dir. My Code is like that Success='code for file... (3 Replies)
Discussion started by: aryan_styles
3 Replies

5. Shell Programming and Scripting

How redirect script output from inside of script?

Is it possible to redirect a script output by command inside of that script? I mean, if I have a script 'dosome.sh' I could run it by >dosome.sh > dosome.log I would dream to get some command inside of scrip to do the same; so, running the dosome.sh would have all output redirected to a log... (4 Replies)
Discussion started by: alex_5161
4 Replies

6. Shell Programming and Scripting

how to redirect the output of a grep command to a file inside a shell script

hi, i wat to get the output of a grep command in a file. but when i am trying out the same grep command in the unix prompt its working fine.. i am getting the output properly.. but when i am writing the same command inside my shell script , its just creating a new output file with no contents... (11 Replies)
Discussion started by: kripssmart
11 Replies

7. Shell Programming and Scripting

redirect output of dos2unix command

hi I want to suppress the output of dos2unix command in my shell script. I'm using follwing command in my script dos2unix somefile >/dev/null But it's still showing output while executing the script.Please help me to sort this out Thanks (4 Replies)
Discussion started by: nrbhole
4 Replies

8. Shell Programming and Scripting

FTP inside a block of code

I need help on the code below. I am getting a compile error syntax error at line 283 : `<<' unmatched Looks like it doesn't like the << on the ftp line below. If I ran the code outside of this block everything work fine, but when I put in a block of code or in a function, I got syntax error. I... (1 Reply)
Discussion started by: leemjesse
1 Replies

9. Shell Programming and Scripting

redirect output of FTP

Hi, I call the FTP command from within a script. The call to FTP looks like this : ftp -n -i -v < $com >> "$logfile" Where $com is a commandfile and I want the output to be redirected to $logfile. If I do this call at the prompt it works perfect. (replacing the variables by there... (4 Replies)
Discussion started by: kcaluwae
4 Replies

10. UNIX for Dummies Questions & Answers

redirect command output to variable

Hi, I am looking for a way to redirect the result from a command into a variable. This is the scenario. Using the find command I will be getting multiple records/lines back. Here is the command I am using: find /”path”/ -name nohup.out -print This now is giving me the paths and file... (1 Reply)
Discussion started by: hugow
1 Replies
Login or Register to Ask a Question