Welcome!
Important information
-- Spectre and Meltdown vulnerabilities
-- Change in MX sources

News
-- MX Linux on social media: here
-- Mepis support still here

Current releases
-- MX-17.1 Final release info here
-- antiX-17 release info here

New users
-- Please read this first, and don't forget to add system and hardware information to posts!
-- Here are the Forum Rules

SOLVED import output from one script to another script?

Here you can exchange scripts that you created or have permission to share with other users.
Post Reply
Message
Author
User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

SOLVED import output from one script to another script?

#1 Post by GDixon » Tue Nov 11, 2014 5:39 pm

Help

i'm getting most of what i need and learning case esac , it's not so bad

if then fi else elif is a cake walk now and nesting is an exercise in indenting and knowing what you need before hand.

grep isn't too bad but awk is killing me

how can I get the out put of something like this into a second script?

Code: Select all

MAIN_TARGET=$(tac $PLACES | grep -m 1 "$SCAN_TARGET_1" | awk '{print $NF}')
I found separating the menu portion from the main scan script (avscan of clam) allows me to get the menu working correctly. If i embed the menu in the scan script funny things happen when you enter a wrong choice.
separating major functions to their own scripts makes thing a lot easier for me to edit and maintain also .

i kinda stripped most things from the scripts to get them running with a minimum of crud for testing.

this is the text file i store the scan targets in | SCAN_TARGET_1 through _5 that grep finds it's info .

Code: Select all

/media/Storage/NewQbToTransfer/

/media/Storage/

/media/Extra/

/media/TvShows/
this is the case menu script that I need the MAIN_TARGET info from for the scan script.
Ignore most the variables I need to clean them up still.

Code: Select all

#!/bin/bash

clear

# Remember to make executable and check permissions

# case menu for avscan

#------------------------------ START : VARIABLES -----------------------------#

search_for="time stamp for script use"                    # search string for time stamp for connect_time
sys_seconds="system time in seconds     $(date "+%s")"    # search string for sys time for connect_time
CONNECT_TIME="/usr/local/bin/virus_scan/connect_time"     # location of connect script

SCAN="/usr/local/bin/virus_scan/avscan_no_menu"
# MENU="/usr/local/bin/virus_scan/menu_no_avscan"
PLACES="/usr/local/bin/virus_scan/places_to_scan"         # txt file of places to scan

SCAN_TARGET_1="/media/Storage/"                           # location to scan
SCAN_TARGET_2="/media/Storage/NewQbToTransfer/"           # location to scan
SCAN_TARGET_3="/media/Extra/"                             # location to scan
SCAN_TARGET_4="/media/TvShows/"                           # location to scan
SCAN_TARGET_5=""                                          # location to scan
SCAN_TARGET_6=""                                          # location to scan
INFO_1="Will start scan on"



Clam_log="$HOME/ScanLog/Clam.log"                                     # location of Clam.log
MOVED="Virus has been moved to:"                                      # Print to screen
VIRUS="$HOME/.VIRUS/"                                                 # directory you want the virus's moved to
HIDE="(hidden directory)"                                             # uncomment if VIRUS is a hidden directory
SCAN_LOG="$HOME/ScanLog/Clam.log"                                     # directory you want the scan log placed

VERSION="11-07-2014"                                                  # used for log
RIGHT_NOW=$(date "+ %a %x %r")                                        # used for log
RIGHT_NOW1=$(date "+ %s")                                             # used for log
TIME_STAMP="Updated on $RIGHT_NOW"                                    # used for log

NO_DRIVE="Failed to scan: Not plugged in and or mounted?           "  # Print to screen
YIKES="Yikes drive not plugged in and or mounted : Nothing to scan"   # Print to screen

SUBJECT="----------- SCAN  REPORT ------------"                       # Used for log
SUBJECT1="time stamp for script use"                                  # Used for tac / greg / awk 
SUBJECT2="----------- SCAN  FAILED -----------"                       # used for log
VIRUS0="---------- NO VIRUS FOUND ----------"                         # used for log and print to screen
VIRUSL1="------------- VIRUS FOUND -------------"                     # used for log
ERRORL2="----------- ERROR   FOUND -----------"                       # used for log
VIRUSP1="----- VIRUS  FOUND : CHECK  LOG -----"                       # Print to screen
ERRORP2="----- ERROR  FOUND : CHECK  LOG -----"                       # Print to screen
exit=?                                                                # Exit status
selection=
MAIN_TARGET=                                                             

#------------------------------- END : VARIABLES ------------------------------#


#--------------------------------- MENU START ---------------------------------#
    
#    until [ "$selection" = "q | Q" ]; do
    until [ "$selection" = "q" ]; do
    echo ""
    echo -e "What / Where would you like to scan for a Virus / Infection $USER?\n"
    echo -e "1 - Scan $SCAN_TARGET_1\n"
    echo -e "2 - Scan $SCAN_TARGET_2\n"
    echo -e "3 - Scan $SCAN_TARGET_3\n"
    echo -e "4 - Scan $SCAN_TARGET_4\n"
#    echo -e "q | Q - quit and take no further action | exit program\n"
    echo -e "q - quit and take no further action | exit menu\n"
    echo -e "waiting on your choice $USER\n"
    read selection
    echo ""
    case $selection in
        1 ) clear
            MAIN_TARGET=$(tac $PLACES | grep -m 1 "$SCAN_TARGET_1" | awk '{print $NF}')
            echo -e "$INFO_1 $MAIN_TARGET $USER\n"           
            sleep 4; clear; $SCAN ;;                ##### $SCAN starts the avscan script, how can I output the #####
                                                    ##### $TARGET_* to the $MAIN_TARGET in the avscan script?  #####




        2 ) clear
            MAIN_TARGET=$(tac $PLACES | grep -m 1 "$SCAN_TARGET_2" | awk '{print $NF}')
            echo -e "$INFO_1 $MAIN_TARGET $USER\n"
            sleep 4; clear ;;
        3 ) clear
            MAIN_TARGET=$(tac $PLACES | grep -m 1 "$SCAN_TARGET_3" | awk '{print $NF}')
            echo -e "$INFO_1 $MAIN_TARGET $USER\n"
            sleep 4; clear ;;
        4 ) clear
            MAIN_TARGET=$(tac $PLACES | grep -m 1 "$SCAN_TARGET_4" | awk '{print $NF}')
            echo -e "$INFO_1 $MAIN_TARGET $USER\n"
            sleep 4; clear ;;
#    q | Q ) clear;exit ;;
         q) clear; exit ;;
        * ) clear; echo; echo -e "INVALID ENTRY : RESTARTING MENU\n" 
            sleep 2; clear ;;
    esac
        done
#--------------------------------- MENU END -----------------------------------#
this is the main avscan script I need to import the MAIN_TARGET info to, again I still need to clean the variables up and have commented out other stuff to nget a minimal working script for testing.

Code: Select all

#!/bin/bash

clear

# Remember to make executable and check permisions

#------------------------------ START : VARIABLES -----------------------------#

search_for="time stamp for script use"                    # search string for time stamp for connect_time
sys_seconds="system time in seconds     $(date "+%s")"    # search string for sys time for connect_time
CONNECT_TIME="/usr/local/bin/virus_scan/connect_time"     # location of connect script

SCAN="/usr/local/bin/virus_scan/avscan_no_menu"
MENU="/usr/local/bin/virus_scan/menu_no_avscan"
PLACES="/usr/local/bin/virus_scan/places_to_scan"

SCAN_TARGET_1="/media/Storage/"                           # location to scan
SCAN_TARGET_2="/media/Storage/NewQbToTransfer/"           # location to scan
SCAN_TARGET_3="/media/Extra/"                             # location to scan
SCAN_TARGET_4="/media/TvShows/"                           # location to scan
SCAN_TARGET_5=""                                          # location to scan
SCAN_TARGET_6=""                                          # location to scan
INFO_1="Will start scan on"

Clam_log="$HOME/ScanLog/Clam.log"                                     # location of Clam.log
MOVED="Virus has been moved to:"                                      # Print to screen
VIRUS="$HOME/.VIRUS/"                                                 # directory you want the virus's moved to
HIDE="(hidden directory)"                                             # uncomment if VIRUS is a hidden directory
SCAN_LOG="$HOME/ScanLog/Clam.log"                                     # directory you want the scan log placed

VERSION="11-07-2014"                                                  # used for log
RIGHT_NOW=$(date "+ %a %x %r")                                        # used for log
RIGHT_NOW1=$(date "+ %s")                                             # used for log
TIME_STAMP="Updated on $RIGHT_NOW"                                    # used for log

NO_DRIVE="Failed to scan: Not plugged in and or mounted?           "  # Print to screen
YIKES="Yikes drive not plugged in and or mounted : Nothing to scan"   # Print to screen

SUBJECT="----------- SCAN  REPORT ------------"                       # Used for log
SUBJECT1="time stamp for script use"                                  # Used for tac / greg / awk 
SUBJECT2="----------- SCAN  FAILED -----------"                       # used for log
VIRUS0="---------- NO VIRUS FOUND ----------"                         # used for log and print to screen
VIRUSL1="------------- VIRUS FOUND -------------"                     # used for log
ERRORL2="----------- ERROR   FOUND -----------"                       # used for log
VIRUSP1="----- VIRUS  FOUND : CHECK  LOG -----"                       # Print to screen
ERRORP2="----- ERROR  FOUND : CHECK  LOG -----"                       # Print to screen
exit=?                                                                # Exit status
selection=
MAIN_TARGET=
                                                               

#------------------------------- END : VARIABLES ------------------------------#


# uncomment 4 lines below to remove old log
# and time stamp new log
# leave commented to append to log
# Changes will be needed in the connect_time
# script or freshclam will be unable to update
# due to the time check

#- REMEMBER TO MAKE NEEDED CHAGES IN CONNECT_TIME SCRIPT IF UNCOMMENTED BELOW -#

#   echo
#   echo -e "Removing old log\n"; sleep 3; clear
#   rm -f "$CLAM_LOG"                                 # Junk old logfile.
#   echo -e "$SUBJECT1 $RIGHT_NOW1\n" >> $SCAN_LOG    # time stamp new log

#- REMEMBER TO MAKE NEEDED CHANGES IN CONNECT_TIME SCRIPT IF UNCOMMENTED ABOVE -#


#-------------------------- CONNECTION : TIME START ---------------------------#

#echo
#echo -e "Testing for internet connection and checking time of last update\n"

#sleep 4


# use this to test for internet connection and to call freshclam if connected
# if not connected then log: not connected and do the scan anyhow
# Will also check to see how long ago virus database has been updated
# and depending on length of time either allow the update or deny the update

#$CONNECT_TIME   # calls script that determines if there is a internet
                # connection and also how long it has been since the
                # virus database has been updated

#-------------------------- CONNECTION : TIME END -----------------------------#


#---------------------------- MAIN PART TO DO SCAN -------------------------------#

if [ -d $MAIN_TARGET -o -f $MAIN_TARGET ]; then
    echo  # just a blank line to make reading easier   
    echo -e  "Getting ready to scan $MAIN_TARGET for viruses\n"
    echo -e  "If any infected files are found they will be\n"
    echo -e  "moved to $VIRUS $HIDE\n"
    echo -e  "Scan Log will be in $SCAN_LOG\n"
    echo -e  "Scan will begin shortly on $MAIN_TARGET, be patient $USER\n"


    echo  >> $SCAN_LOG    # inserts blank line in log (for layout and formatting)
    echo $SUBJECT >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION - for user: $LOGNAME" >> $SCAN_LOG
    echo -e "Scanmode: Recursive\nFiles Scanned: $MAIN_TARGET\n$TIME_STAMP\n" >> $SCAN_LOG


clamscan -r --move=$VIRUS $MAIN_TARGET -l $SCAN_LOG     # clamscan command and arguments/options

                         ##### how can i import the $SCAN_TARGET_* info from the menu script?



              

              if [ "$?" -eq "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG        # inserts 2 blank lines in log (for layout and formatting)
                      echo $VIRUSL1 >> $SCAN_LOG
                      echo -e "$MOVED\n$VIRUS\n$HIDE\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUSP1"             

                    elif [ "$?" -gt "1" ]; then
                      echo -e "\n"  >> $SCAN_LOG
                      echo $ERRORL2 >> $SCAN_LOG
                      echo
                      echo -e "$ERRORP2"

                    else
                      echo -e "\n"  >> $SCAN_LOG
                      echo  -e "$VIRUS0\n" >> $SCAN_LOG
                      echo
                      echo -e "$VIRUS0"
              fi


# above logging is used when drive/directory is present

sleep 10; clear

# below is used for log when drive is missing or not mounted

else
    echo
    echo -e "$YIKES\n"
    echo >> $SCAN_LOG    # inserts a blank line in log (for layout and formatting)
    echo $SUBJECT2  >> $SCAN_LOG
    echo -e "Script: $(basename $0) v$VERSION - for user: $LOGNAME" >> $SCAN_LOG
    echo -e "Missing target $MAIN_TARGET\n$NO_DRIVE\n$TIME_STAMP\n" >> $SCAN_LOG

       sleep 4; clear

fi

    exit
        done
lots of clean up left but it will run , problem is without the MAIN_TARGET getting imported avscan falls back to scanning the home directory.

what am I missing and just haven't got my mind wrapped around yet?
Any suggestions or a direction to try?
Last edited by GDixon on Tue Nov 11, 2014 10:47 pm, edited 1 time in total.

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: how do I import output from one script to another script

#2 Post by GDixon » Tue Nov 11, 2014 7:32 pm

I think I found a direction

Make the variable an environment variable (export TESTVARIABLE) before executing the 2nd script.

I'm going to give this a try and see what happens.

I'm still open to suggestions and or other directions I can take in doing this.

User avatar
Old Giza
Forum Regular
Forum Regular
Posts: 360
Joined: Wed Apr 16, 2014 10:31 pm

Re: how do I import output from one script to another script

#3 Post by Old Giza » Tue Nov 11, 2014 9:40 pm

When you don't want to use external files, and piping doesn't offer a fine enough control, "process substitution" can be useful. It is particularly good when you need to examine the output of a command (or script) line by line within a while/read/do/done loop.

This may be more than you need. But when you do have a situation where it can be used, it can save all sorts of convoluted work-arounds.

http://www.tldp.org/LDP/abs/html/process-sub.html

Note the warning about "no space between the '<' and the parentheses".

User avatar
GDixon
Forum Regular
Forum Regular
Posts: 358
Joined: Fri Nov 02, 2007 4:39 pm

Re: how do I import output from one script to another script

#4 Post by GDixon » Tue Nov 11, 2014 10:40 pm

export as a system variable was the answer and it allowed me to clean a lot of crud out of the scripts.
I imagine I could put all my variables in the menu script and export them all as system variables but the seems counter to having them in the scripts they belong in.

This is my first menu using case over if then else and it seems cleaner and easir but not by a lot, not sure I will prefer one way over the other in time.

heres a sample of the code for the menu to export what I needed which also got rid of a lot of convoluted crud, super simple once you find out how.

Code: Select all

1 ) clear; echo
            echo -e "$INFO_1 $SCAN_TARGET_1 $USER\n"           
            sleep 3; clear
            export MAIN_TARGET=$SCAN_TARGET_1; $SCAN ;;

Post Reply

Return to “Scripts”