Storing o/p of a command to a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Storing o/p of a command to a variable
# 1  
Old 09-16-2010
Storing o/p of a command to a variable

Hi,

I have a ftp script there I want to store the o/p of the below command:
Code:
sftp -b <batch file> user@password 
cat <batch file>
get /remote/file/path/remote_file_name.csv*.gz  /local/path

Now the problem is that when I fire this command. Then it gives o/p as:
Code:
 
File "/remote/file/path/remote_file_name.csv*.gz" not found.

but when in the script I fire the same command as below:
Code:
 
fetch_file=`sftp -b <batch file> user@password`

then it doesn't store the error i.e.
Code:
File "/remote/file/path/remote_file_name.csv*.gz" not found.

but stores only
Code:
echo $fetch_file 
sftp> get /remote/file/path/remote_file_name.csv*.gz  /local/path

Can anyone please suggest how store the error in the variable as well?

-dips
# 2  
Old 09-16-2010
Why don't you redirect the output to a log file and after, parse the log file?
Like:
Code:
sftpLogFile="SFTPLog.log"
sftp -b <batch file> user@password 1>"${sftpLogFile}" 2>&1
if [ $? -ne 0 ]
then
    echo "SFTP Error."
else
    echo "SFTP Ok."
fi

# 3  
Old 09-16-2010
Hey felipe,

The problem is that I don't want black or white scenario......Smilie

As I wouldn't be sure of the exact file name; I want to capture it so I had to use something like this
Code:
fetch_file=`sftp -b <batch file> user@password`
echo fetch_file_succ=$?

else I would just used
Code:
sftp -b <batch file> user@password
echo ftp_succ=$?

But when it successfully finds the file on the FTP server then the variable has the below value:

Code:
echo $fetch_file
sftp> get /remote/file/path/remote_file_name.csv*.gz /local/path Fetching /remote/file/path/remote_file_name.csv_1797.gz  to /local/path/remote_file_name.csv_1797.gz sftp>

And after that I get the exact name of the file as below:

Code:
File_Name_Path=`echo $fetch_file | sed 's/sftp>//g' | awk '{print $NF}'`
File_Name=`basename $File_Name_Path`
echo $File_Name
remote_file_name.csv_1797.gz

-dips
# 4  
Old 09-16-2010
OK!

After continuing with your solution, why don't you use ssh to list the file and use scp to copy it to the localhost?

Code:
# To list
echo "ls -l /remote/file/path/remote_file_name.csv*.gz" | ssh -C -T -l <User>

# To get the file
scp <User>@<Host>:<FileFromAbove> <LocalPath>

Regards!
# 5  
Old 09-16-2010
Oh no! I cannot change the way the FTPing is done in the script.....it's already in Production env and because of some environment issues we had to fix a bit here and there.

So I cannot change from "sftp" to "ssh" & "scp" sorry! But can tweak few unix commands to work this out!

And also when I do this:
Code:
fetch_file=`sftp -b <batch file> user@password`
echo fetch_file_succ=$?

It shows success i.e. (fetch_file_succ=0) even when there's a failure!!

-dips
# 6  
Old 09-16-2010
If I understood what you want, you can use both to solve your problem.

Something like:
Code:
fetch_file=`sftp -b <batch file> user@password`
echo fetch_file_succ=$?

if [ ${fetch_file_succ} -ne 0 ]
then
	echo "SFTP Failed!"
	exit 1
fi

File_Name_Path=`echo $fetch_file | sed 's/sftp>//g' | awk '{print $NF}'`
File_Name=`basename $File_Name_Path`
echo $File_Name

But, you are saying that you cannot trust sftp's return code.

Can you post the error SFTP is returning to you?

In the past, all my routines were based on FTP, but I could not trust on the return code, so I redirected its output to a file and did some validations on it and that's why I went to scp.

I had some sftp issues, that, even when the transfer was ok, it returned an error code != 0, just because I issued a mkdir inside the batch file, and the directory I was creating already exist in the remote host.

Have you tried to raise the sftp logging level?
Code:
fetch_file=`sftp -vvv -b <batch file> user@password`

I am going to do a test and post the result here.

---------- Post updated at 13:16 ---------- Previous update was at 13:07 ----------

Ok!

If the above helps, you can do the following to validate the error:
Code:
# Redirects the STDERR to STDOUT with 2>&1
var=`sftp -b ./testSFTPBatch.txt transf_operacao@gn031cb 2>&1`
sftpRetCode=$?

# After validate the variable:
errCount=`echo $var | egrep -i 'not found' | wc -l`

if [ ${errCount} -ne 0 -o ${sftpRetCode} -ne 0 ]
then
     echo "SFTP Failed!"
     exit 1
fi

# 7  
Old 09-20-2010
Hey felipe,
I was able to solve my problem by doing the below:

Code:
original code:
---------------
fetch_file=`sftp -b <batch file> user@password` 2>>$logFile
 
I changed it to 
----------------
fetch_file=`sftp -b <batch file> user@password 2>>$logFile`

So that it captured non-zero exit status. I don't know what was happening but with original code it always use to return 0 (success)

probably a stupid mistake!! Smilie

-dips
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Storing multiple sql queries output into variable by running sql command only once

Hi All, I want to run multiple sql queries and store the data in variable but i want to use sql command only once. Is there a way without running sql command twice and storing.Please advise. Eg : Select 'Query 1 output' from dual; Select 'Query 2 output' from dual; I want to... (3 Replies)
Discussion started by: Rokkesh
3 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Storing awk command in a variable

I'm working on a script in which gives certain details in its output depending on user-specified options. So, what I'd like to do is something like: if then awkcmd='some_awk_command' else awkcmd='some_other_awk_command' fi Then, later in the script, we'd do something like: ... (5 Replies)
Discussion started by: treesloth
5 Replies

4. Shell Programming and Scripting

Storing command output in a variable and using cut/awk

Hi, My aim is to get the md5 hash of a file and store it in a variable. var1="md5sum file1" $var1 The above outputs fine but also contains the filename, so somthing like this 243ASsf25 file1 i just need to get the first part and put it into a variable. var1="md5sum file1"... (5 Replies)
Discussion started by: JustALol
5 Replies

5. Shell Programming and Scripting

Storing output of "time" command to a variable

Hi all, I am new to Linux/shell scripting having moderate knowledge. In my script, I need to get execution time of a command (say 'ls') in mili seconds level. For this i tried using "time" command to retrieve the total execution time in milli seconds. But, the problem is that, how to save... (9 Replies)
Discussion started by: happening_linux
9 Replies

6. Shell Programming and Scripting

About storing the value of wc -l into a variable and then using this value in while

Hi all, I m new to this forum. I ma facing onei issue. I have something like this: length= wc -l < b2| awk '{print $1}' where b2 is filename having detauls like: cat b2 abc1 abc4 xyc3 sbdghf4 but when I do echo "$length" it displays nothing Also I am using awk to overcome... (4 Replies)
Discussion started by: student2009
4 Replies

7. Shell Programming and Scripting

Storing value in a variable

Hi Everyone, I have a code which requires to be stored in different variables and I am achiving it like this. HOST=`echo $RMP | cut -f2 -d:` NAME=`echo $RMP | cut -f3 -d:` DIR=`echo $RMP | cut -f4 -d:` TYPE=`echo $RMP | cut -f5 -d:` Is there any other way of storing value... (2 Replies)
Discussion started by: gehlnar
2 Replies

8. Programming

Need help in storing command line argument argv[2] to a variable of int type

The following program takes two command line arguments. I want the second argument (fileCount) to be stored/printed as a int value. I tried my best to typecast the char to int (check the printf statement at last) but is not working...the output is some junk value. This program is in its... (3 Replies)
Discussion started by: frozensmilz
3 Replies

9. Shell Programming and Scripting

storing a command in a variable

how would i go about storing this command in a variable echo "$LINE" | awk -F"|" '{print $1"|"$2"|"$3"}' i have tried FOO = ${command up there} but receive the error FOO: not found aswell as a couple of other attempt but no luck (2 Replies)
Discussion started by: nookie
2 Replies

10. Shell Programming and Scripting

storing result of a command in variable

For whatever reason I cant seem to fix my syntax to do the following. I want to run a grep and count how many instances come up and store that number in a variable but I keep erroring out. Here's my code in bash: number=grep blah file.txt | wc -l (1 Reply)
Discussion started by: eltinator
1 Replies
Login or Register to Ask a Question