Page 1 of 5 123 ... LastLast
Results 1 to 10 of 44

Thread: bash script code error on command output to varible

  1. #1

    Default bash script code error on command output to varible

    Code:
         #create LinuxScanFolders cfg file if not present
         if [[ -f "/var/log/VirusVault/scanvirus.cfg" ]]; then
            printf ""
         else
            printf "creating LinuxScanFolders\n"
            cat > /var/log/VirusVault/scanvirus.cfg <<EOL
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    date '+%Y-%m-%d %I:%M%P'
    ___________________________________________
    EOL
         fi
    
         #setup configuration lines into array
         IFS=$'\n' read -d '' -r -a lines < /var/log/VirusVault/scanvirus.cfg
    
         printf "excluded folders: %s\n" "${lines[3]}"
         printf "time date stamp : %s\n" "${lines[6]}" 
            
        
         Time_Date_Stamp=$( "${lines[6]}" ) 
         # check for date command error
         #if [[ $? != 0 ]]; then
         #     echo "----- Time_Date_Stamp error -----"
         #     exit 1
         #fi
    
         printf "%s\n" $Time_Date_Stamp
    It checks for the config file. Creates it if needed. Then reads the file lines into an array. Assigns the time_date_stamp from a line in the file.

    Code:
    excluded folders: etc dev proc tmp mnt media srv .snapshots
    time date stamp : date '+%Y-%m-%d %I:%M%P'
    ./scanvirusa: line 136: date '+%Y-%m-%d %I:%M%P': command not found
    I can't find the error. Need help.

  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    28,528
    Blog Entries
    15

    Default Re: bash script code error on command output to varible

    Hi
    Missing backticks....

    Code:
    `date '+%Y-%m-%d %I:%M%P'`
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  3. #3

    Default Re: bash script code error on command output to varible

    Quote Originally Posted by malcolmlewis View Post
    Hi
    Missing backticks....

    Code:
    `date '+%Y-%m-%d %I:%M%P'`
    I replaced date in the CFG file. It still errors.
    Code:
    `date '+%Y-%m-%d %I:%M%P'`
    Code:
    Time_Date_Stamp=$(`("${lines[6]}")`) 
    Time_Date_Stamp=$(`"${lines[6]}"`)
    
    ./scanvirusa: line 136: date '+%Y-%m-%d %I:%M%P': command not found

  4. #4
    Join Date
    Mar 2020
    Location
    São Leopoldo, RS, Brazil
    Posts
    106

    Default Re: bash script code error on command output to varible

    Quote Originally Posted by lord_valarian View Post
    I replaced date in the CFG file. It still errors.
    Code:
    `date '+%Y-%m-%d %I:%M%P'`
    Code:
    Time_Date_Stamp=$(`("${lines[6]}")`) 
    Time_Date_Stamp=$(`"${lines[6]}"`)
    
    ./scanvirusa: line 136: date '+%Y-%m-%d %I:%M%P': command not found
    From the error message we can infer bash is taking the whole line as the command name, whereas we want only "date". You could remove double quotes from the command, but from a quick test it seems it's doing word splitting, ignoring the single quote. I wish I knew why.
    If you can change the format in the .cfg file, I suggest you omit the "date" part, and prepend the date command before the quoted line. Not a definitive solution, but may work in your case.
    openSUSE Tumbleweed

  5. #5
    Join Date
    Sep 2012
    Posts
    5,503

    Default Re: bash script code error on command output to varible

    Quote Originally Posted by awerlang View Post
    it seems it's doing word splitting, ignoring the single quote. I wish I knew why.
    Code:
         Time_Date_Stamp=$( "${lines[6]}" )

  6. #6

    Default Re: bash script code error on command output to varible

    Code:
    date '+%Y-%m-%d %I:%M%P' 2020-04-23 10:53pm
    Code:
    Time_Date_Stamp=$(date '+%Y-%m-%d %I:%M%P') printf "%s\n" $Time_Date_Stamp
    Code:
    2020-04-23 10:55pm
    command output to string is adding a newline.
    Code:
    printf -v Time_Date_Stamp "%s" $(date '+%Y-%m-%d %I:%M%P') printf "%s\n" $Time_Date_Stamp
    Code:
    2020-04-2310:59pm
    ARG! (sigh)

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    26,206

    Default Re: bash script code error on command output to varible

    Quote Originally Posted by malcolmlewis View Post
    Hi
    Missing backticks....

    Code:
    `date '+%Y-%m-%d %I:%M%P'`
    He has $( date '+%Y-%m-%d %I:%M%P' ), which is a more modern (for 30 years at least) and better readable form of ` .... `.
    Henk van Velden

  8. #8
    Join Date
    Mar 2020
    Location
    São Leopoldo, RS, Brazil
    Posts
    106

    Default Re: bash script code error on command output to varible

    Quote Originally Posted by arvidjaar View Post
    Code:
         Time_Date_Stamp=$( "${lines[6]}" )
    Not sure what you mean by that, the single quote I refer to is inside the .cfg file. With the double quotes above word splitting doesn't occur.
    openSUSE Tumbleweed

  9. #9
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    28,528
    Blog Entries
    15

    Default Re: bash script code error on command output to varible

    Hi
    Code:
    #!/bin/bash
    
            cat > scanvirus.cfg <<EOL
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    date '+%Y-%m-%d %I:%M%P'
    ___________________________________________
    EOL
    
            cat >> scanvirus.cfg <<EOL
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    `date '+%Y-%m-%d %I:%M%P'`
    ___________________________________________
    EOL
    
    cat scanvirus.cfg
    
     ./scan_test
    
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    date '+%Y-%m-%d %I:%M%P'
    ___________________________________________
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    2020-04-24 09:19am
    ___________________________________________
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  10. #10
    Join Date
    Mar 2020
    Location
    São Leopoldo, RS, Brazil
    Posts
    106

    Default Re: bash script code error on command output to varible

    Quote Originally Posted by malcolmlewis View Post
    Hi
    Code:
    #!/bin/bash
    
            cat > scanvirus.cfg <<EOL
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    date '+%Y-%m-%d %I:%M%P'
    ___________________________________________
    EOL
    
            cat >> scanvirus.cfg <<EOL
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    `date '+%Y-%m-%d %I:%M%P'`
    ___________________________________________
    EOL
    
    cat scanvirus.cfg
    
     ./scan_test
    
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    date '+%Y-%m-%d %I:%M%P'
    ___________________________________________
    __________scanvirus configuration__________
    ___________________________________________
    Excluded Scan Folders
    etc dev proc tmp mnt media srv .snapshots
    ___________________________________________
    Time Date Stamp
    2020-04-24 09:19am
    ___________________________________________
    The backticks are evaluated at the time of "cat <<EOL", it means the .cfg file holds now a timestamp instead of a format. To avoid that, use "cat <<'EOL'" (single quote EOL) but that leads to the original issue.
    openSUSE Tumbleweed

Page 1 of 5 123 ... LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •