0
echo -e '(b) \033[3;32m  for between hours\033[m';
echo -e ' please choose \033[32;5m (a or b)\033[m';
read option
#if [ "$option" == "a" ]
#then
        destination="/home/siva/test"
        echo -e 'date in format: \033[33;5m(mm-dd-yy) eg.10-30-22\033[m '       # date format of thread_dumps needed
        read -p "enter date : " D                               #enter the date of thread_dumps needed
        echo -e 'hour in format: \033[33;5m( HH )\033[m'
        read -p "enter Time (HH:MM) : " T   #enter specific hour of thread_dumps needed
        H= echo $T | awk '{print substr($1,1,2)}'

        find=`find /home/siva/thread_dumps/thread_dump_"$D"_"$H"\:[0-9][0-9]\:0[1-2]`

        echo $find
        #cp $find $destination

       # zip -r /home/siva/zip/thread_dumps_"$D".zip $find

In the above script I want to assign the variable (H= echo $T | awk '{print substr($1,1,2)}). And I want to use the variable in the same script. But this doesn't work for me. I want echo output in a variable and use it in find command

3
  • Welcome to the site. I would recommend that you check your script with shellcheck, also available as stand-alone program in many Linux distributions, because it contains various syntax errors on the line where you want to assing H.
    – AdminBee
    Oct 18, 2022 at 12:26
  • 1
    There doesn't really seem to be any use for the find command. You appear to want to use a globbing pattern (which includes the two variables $D and $H) to match a number of directories under /home/siva/thread_dumps, is this correct? Also, if you want the hour in "HH" format, why do you ask for the time in "HH:MM" format?
    – Kusalananda
    Oct 18, 2022 at 12:28
  • Please edit your question and tell us what you want to assign $H to. Should it have the string echo $T | awk '{print substr($1,1,2)}' or should it have the result of running the command echo $T | awk '{print substr($1,1,2)}'? Also note you cannot have spaces around the = in variable assignments.
    – terdon
    Oct 18, 2022 at 12:35

1 Answer 1

0

I'm not entirely sure, but it looks as if you don't need find in your code. You also don't need to do user interaction if you instead take the user's input from the command line arguments:

#!/bin/sh -u

# Usage:
#
# ./script MM-DD-YY HH
#

archive=$HOME/zip/thread_dumps_${1}.zip

if [ -e "$archive" ]; then
        printf '"%s" already exists\n' "$archive" >&2
        exit 1
fi

for dirpath in "$HOME/thread_dumps/thread_dump_${1}_${2}":??:??
do
        if [ -d "$dirpath" ]; then
                printf 'Processing "%s"\n' "$dirpath" >&2
                zip -r "$archive" "$dirpath"
        fi
done

This script takes the date and the hour from the arguments given by the user on the script's command line. It then uses basic filename globbing to match the directories that should be archived and does so one directory at a time. Modify the pattern to fit your needs.

A small change to this would be to make sure that the path $HOME is not stored in the archive so that the archive unzips into a tread_dumps archive.

You can do that by changing the loop into

for dirpath in "$HOME/thread_dumps/thread_dump_${1}_${2}":??:??
do
        if [ -d "$dirpath" ]; then
                printf 'Processing "%s"\n' "$dirpath" >&2
                ( cd "$HOME" && zip -r "$archive" "${dirpath#$HOME/}" )
        fi
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .