2

I work in support and am creating a script that allows a user to quickly gather information for diagnostic reasons.

I have multiple searches and choose to use the case method inside a .sh

main() {
#What we looking for?
echo "Whats broken? ( Key , Level, Bot, LPN, Variant, Starvation, Disconnects"
echo "entering \"helplist\" will print and list of available choices"
echo "Enter quit to quit or just control+c =-)"
read broken

#push to lowercase :p broken2="${broken,,}" #makes log files

case "$broken2" in "stuck task") echo "Task Number?" read -r taskid grep --color=auto "id=$taskid" "$engine"/TaskAssignment.txt | tee ~/$broken2.task$taskid.txt ;;

esac printf "\n\n" main } main

The tee itself works fine but I'd like to be able to generate the file based on the case and the argument passed into it.

How can you get the value of the case into the tee command read $case ?

Alex R
  • 51
  • What did you use for the case statement? If it's not in a variable, then make it so, and reuse the variable? – Attie Aug 11 '20 at 17:59
  • Such an easy oversight... Trying now. – Alex R Aug 11 '20 at 18:13
  • Didn't work. Can't get the code to format right in the comments so re-edited original post to include original read – Alex R Aug 11 '20 at 18:38
  • I'd presume (from the snippet you've provided) that $broken2 contains a space, so because you didn't quote the arguments to tee, you'll have two files - ~/stuck and ./task.task$taskid.txt. It's also wise to use the ${var} syntax instead of $var when building a string, to be more precise variable naming. – Attie Aug 11 '20 at 19:17
  • Got it! Two lines edited in the file case ${broken2} in tee ~/"${broken2}".$taskid.txt – Alex R Aug 11 '20 at 19:34
  • Great! Quote the whole thing (i.e: "~/${broken2}.task${taskid}.txt"), and feel free to submit an answer :-) – Attie Aug 11 '20 at 19:47

1 Answers1

1

By reading the case as a variable it will be passed down into the case itself

Case is stored as broken2 but since some cases will contain multiple words we wrap them in brackets

case ${broken2} in

grep --color=auto "id="$taskid"" $engine/TaskAssignment.txt | tee -a ~/"${broken2}".$taskid.txt

Running the script with ./tool.sh
broken2 was fed stuck task
taskid was fed 762540
file output stuck task.762540.txt

Alex R
  • 51