cancel
Showing results for 
Search instead for 
Did you mean: 

SAP BODS: read a list of files on Linux and store file names in a variable, and send email

LeeFung
Participant
0 Kudos

Hi gurus, how can I set BODS job to read a folder on Linux, and store all file names in a variable, and send an email including all the file names? Thanks!!!

View Entire Topic
jmuiruri
Product and Topic Expert
Product and Topic Expert
0 Kudos

Greetings lifengwu,

1. Create an external script that you can call from the job to get the list of files in a particular directory

Example: below i have a Linux script called list_files.sh that takes in the target directory as an input parameter

[~]$ cat list_files.sh
#!/bin/env sh
ls $1 | awk 'NR==1 {printf "%s", $0; next} {printf ";\n%s", $0} END {print ""}'

2. In the designer create a Job and in the Job add a Script object. In the script you need to use the exec function to call the Linux shell script.

# call the shell script and store the output to a variable $RAW_OUTPUT
$RAW_OUTPUT = exec('sh', '/home/gcpcloud/list_files.sh /home/gcpcloud/',8);

# Remove any blanks and control characters from the start and end of output and store the result in a variable
$REM_CHARS = ltrim_blanks_ext(rtrim_blanks_ext($RAW_OUTPUT));

# Remove the first three characters which represent the return code from exec function, a colon and a space separator
$CLEANED_OUTPUT = substr($REM_CHARS , 3, length($REM_CHARS));

# send email with the list of the files
smtp_to( 'dsamd@psgbods.sap', 'test linux',$CLEANED_OUTPUT,50,50 );

3. Output from running the above script

4. Mail Sent successfully

Please note that smtp_to() function does not support html emails and so the message body will be a one liner statement. to use html emails you need to use a different mailing utility that you can call using the exec function.

Best Regards,

Joseph