Assigning basename result to another variable

This is a two part request for an assistance.

I am not sure how retrieve value from basename command - line 270 -so in can be output as variable CLI_COMMAND - line 250 in whiptail input box.

As coded I can input from keyboard ( stdin?) into input box using redirection.

I can extract wanted values using "do dir..." and print them to terminal - stdout.

What I need help with is assigning results of "do dir..." namebase to another variable.

Perhaps the answer is in output redirection, but I do not know where in the code it should be applied.

# start of menu 13 processing 

   	 	 	 	   13)
  248                echo "TEMPLATE Option 13"
  249 #stupid spaces AGAIN  
  250 CLI_COMMAND=$(whiptail --title "Test Free-form Input Box" \
  251 --inputbox "Enter command " 10 60 usb_devices   3>&1 1>&2 2>&3)
  252 # 1>&1 2>&2 3>&3  
  253 exitstatus=$?
  254 if [ $exitstatus = 0 ]; then
  255     echo "Processing commmand " $CLI_COMMAND
  256 else
  257     echo "You chose Cancel."
  258 fi
  259 echo "process command START " $CLI_COMMAND
  260 $CLI_COMMAND     #TOK  
  261 pause   
  262  echo "process command END " $CLI_COMMAND
    263 pause  
  264 # extract value at * (?)  
  265 # for  directory  *  
  266  for dir in /sys/class/net/*/wireless; do
  267 #if the file *  is -d directory (?)  
  268     if [ -d "$dir" ]; then
  269 #extract only   base , no extensions  
  270       basename "$(dirname "$dir")"
  271       echo $?   # print  exist status 0  OK   
  272 # don;t work print empty line echo   "$dirname"  
  273 #echo  entire path  
  274       echo "echo  entire path here " $dir  
  275       if [ "$debug" = true ]
  276          then  
  277          echo "Printed list_wlan_interfaces" $dir  
   
  278          pause  
  279       fi
  280     fi
  281   done
  282                 ;;
  283 #done with menu 13 

  284         14)
 
 
 

------ Post updated 08-12-18 at 07:42 AM ------

FYI

The code was pasted from three parts taken form nano editor via LibreOffice because I could not copy it in one piece in nano.

Any time you want to assign a value to a variable in a shell script using Bourne shell syntax (you haven't told us what operating system and shell you're using, but from what you have shown us I assume you're using a shell that is based on Bourne shell syntax), you use a shell variable assignment statement, i.e.:

variable=value

If you want to get the next to the last component of a pathname containing at least two components using the dirname and basename utilities, you could use something like:

pathname=/path/name/to/file
next2last=$(basename "$(dirname "$pathname")")
printf 'Extracted directory name is: "%s"\n' "$next2last"

which produces the output:

Extracted directory name is: "to"

With any shell that supports the shell variable expansions required by the POSIX standards, one can get a faster response avoiding the two subshells and two invocations of external utilities by just using features of the shell programming language:

pathname=/path/name/to/file
pathwithoutfinal=${pathname%/*}
printf 'After performing equivalent of dirname $pathname, we have: "%s"\n' "$pathwithoutfinal"
next2last=${pathwithoutfinal##*/}
printf 'Extracted directory name is: "%s"\n' "$next2last"

which produces the output:

After performing equivalent of dirname $pathname, we have: "/path/name/to"
Extracted directory name is: "to"

Note also that the comments in your code do not come close to describing what the code you have shown us actually does. This makes it hard to figure out what you are really trying to do. For example, your comments talk about removing extensions, but none of the code you have shown us makes any attempt to remove filename extensions. :confused:

I will try to clarify one of my issues.
I need to get item selected from whiptail (menu) --checklist

Here is my last simplified code using "while read" and it sort off works retrieving values from menu.

Three issues

  1. It will print continually - after "enter" and only first parameter passed to the function , not the (menu) selection and only when --separate-output is implemented

  2. It will print selection in "" after "enter", then it will print first parameter passed to the function, continually after each enter press when --separate-output is NOT implemented.

  3. It will never "terminate " the "while read "

I can live with "" around selection , but need to stop the "while read".
Any suggestion will be appreciated.
Cheers

BTW

I run plain bash on Raspian OS.

My code is under construction / troubleshooting and I do concentrate on issues
not on keeping the code in sync with comments. It should not be a big issue if discussion participants bypass the comment too if the are not relevant.

   	 	 	 	   czechlist_DEBUG  
 Passed parameter #1 is DEBUG USB  
 Passed parameter #2 is lsusb
 Passed parameter #3 is test parameter !  
 Passed parameter #4 is test parameter 2  
 
 
 DEBUG USB  
 
 
 DEBUG USB  
 
 
 
 
 
 
 czechlist_DEBUG(){
 echo "czechlist_DEBUG "
 echo "Passed parameter #1 is $1"
 echo "Passed parameter #2 is $2"
 echo "Passed parameter #3 is $3"
 echo "Passed parameter #4 is $4"
 #echo "--checklist <text> <height> <width> <listheight> [tag it$
 # Option 1"
 #pause  
  whiptail \
  --title "$1"  \
  --separate-output \
  --checklist  "Choose: " 20 78 15 \
  "$2" "" on  \
  "$3" "" off \
  "$4" "" off
  2>selection  
 #prints selection / results without separate-output option
 #  --separate-output \
 while read selection 
 do  
         echo "$1"
 done  
   p { margin-bottom: 0.1in; line-height: 115%; }p { margin-bottom: 0.1in; line-height: 115%; }