Assigning basename result to another variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assigning basename result to another variable
# 1  
Old 08-12-2018
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.





Code:
# 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.
# 2  
Old 08-13-2018
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.:
Code:
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:
Code:
pathname=/path/name/to/file
next2last=$(basename "$(dirname "$pathname")")
printf 'Extracted directory name is: "%s"\n' "$next2last"

which produces the output:
Code:
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:
Code:
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:
Code:
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. Smilie
# 3  
Old 08-13-2018
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.





Code:
   	 	 	 	   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%; }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Basename for directory variable

hi all, trying to get this to work but im struggling abit and wondered if you can help me out basically i have created a variable base='basename $dir' echo "please specify full path to directory you want to be made into a tar" read -e dir tar -cf... (7 Replies)
Discussion started by: robertkwild
7 Replies

2. Shell Programming and Scripting

Assigning variable to output gives error with expected result

Hello, I am trying to print out the first string matching query with grep and I need your help. My scenario: Database John F 4433 Street No 88 CA Elisabeth Taylor 7733 Street No 26 ON Jack Nicholson 0133 Green Park No 34 AR John F 2 9399 Southpark No 02D UT test.sh... (6 Replies)
Discussion started by: baris35
6 Replies

3. Shell Programming and Scripting

Assigning a variable

I have a date column as 06302015 but I need to have variable which extracts 063015. Am trying something like below but it is not assigning Please let me know if am missing something. Thanks in advance. ################################ #!/usr/bin/ksh DT=06302015 ... (7 Replies)
Discussion started by: weknowd
7 Replies

4. Shell Programming and Scripting

Assigning value to a variable

Unable to get the value to a variable. set -x cd $HOME echo "Enter the server name" read a echo $a i=4 j=1 k = ps -ef | awk '/server1/{ print $4 }' | tail -$i | head -$j` echo $k When I do the same in command line it works, however the same does not work when I provide that in the... (1 Reply)
Discussion started by: venkidhadha
1 Replies

5. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

6. Shell Programming and Scripting

Removing a character from a variable and assigning it to another variable?

Hi folks. I have this variable called FirstIN that contains something like this: 001,002,003,004... I am trying to assign the content of this variable into ModifiedIN but with the following format : 001 002 003 004...(changing the commas for spaces) I thought about using sed but i am not... (17 Replies)
Discussion started by: Stephan
17 Replies

7. Shell Programming and Scripting

Assigning value to a variable

can we make a global variable and store character values and add other values to that variable ?? for example a="hello, John" and can we add value ". How are you? so a can have "hello, John. How are you?" can someone help me?? (2 Replies)
Discussion started by: bonosungho
2 Replies

8. Shell Programming and Scripting

assigning a variable

hi all, in ksh, how do i assign the output of a find command to a variable, e.g am trying something like this : totalNoFiles=$(print find ./ -name "SystemOut*.log"); but when i echo $totalNoFiles it displays find ./ -name "SystemOut*.log" instead of the total number of... (2 Replies)
Discussion started by: cesarNZ
2 Replies

9. Shell Programming and Scripting

Setting basename and dirname variable to simply script.

Hello all, Can somebody explain to me how set up a basename and dirname variable to simplify this script. I currently have a 'infile' with the contents of FTTPDataPVC_ & BaaisDSLFeed. I need to add a basename and or dirname variable so that any additions can be made through the infile and not... (1 Reply)
Discussion started by: liketheshell
1 Replies

10. Shell Programming and Scripting

Assigning a value to variable

Another newbie to Unix scripting Q.. How do you assign a value resulting from a command, such as awk, to a variable. I am currently trying:- $awk '{print $1}' file1 > variable1 with no change to $variable1. The line: $awk '{print $1}' file1 does print the first line of the... (3 Replies)
Discussion started by: sirtrancealot
3 Replies
Login or Register to Ask a Question