LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 02-13-2008, 12:19 AM   #1
craftybytes
Member
 
Registered: Jan 2006
Location: Killarney, QLD Australia
Distribution: Mepis 3.4-3; Puppy 2.12; WinXP Pro (rarely)
Posts: 54

Rep: Reputation: 15
Bash script - how to get 'basename' command to do what I want..?(SOLVED)


Hi script guru's..

Posted this earlier on the "Non-*NIX" section of this forum, but felt that I might get more response in this section instead - sorry for double posting..!!

I need some help with my script..(duh)..

I'm having trouble trying to work out how to use the "basename" command to perform what I want..

Here's the section of my script that needs review:

Code:
#---------------------------------
#           Decoding
#---------------------------------
else  # if [ "$a" == "bfe" ]
Xdialog --screen-center --wrap --no-buttons --title "INFO" --infobox "Decoding file.!" 160x50 2000 >&1
wait1
bcrypt $FILE < "$FIL2" 2>/dev/null
 if [ $? -eq 1 ];then
   error2
 else
 Xdialog --screen-center --wrap --no-buttons --title "INFO" --infobox "Pass 1 completed.!" 180x50 2000 >&1
 wait1
 FILE=`basename "$FILE" .bfe` 
 mv "$FILE" `basename "$FILE" .bfx`.bfe
 wait1
 pkey1
 wait1 
 bcrypt `basename "$FILE" .bfx`.bfe < "$input" 2>/dev/null
  if [ $? -eq 1 ];then
    erpkey
    bcrypt `basename "$FILE" .bfx`.bfe < "$input1" 2>/dev/null
    if [ $? -eq 1 ];then
      error3
    else
      echo "OK"
      Xdialog --screen-center --wrap --no-buttons --title "INFO" --infobox "Decoding completed.!" 180x50 3000 >&1
      rm -f $input
      rm -f $input1
      exit 0
    fi
  else
    rm -f $input
    Xdialog --screen-center --wrap --no-buttons --title "INFO" --infobox "Decoding completed.!" 180x50 3000 >&1
    exit 0
  fi
 fi
fi
The 'wait1' 'error2' 'pkey1' 'erpkey' 'error3' 'opcanc' are internal functions which are defined at the top of the main script..


Here is the 'error3' function that I would like to change :

Code:
function error3 {
Xdialog --screen-center --title "ERROR" --msgbox "Still incorrect personal encryption key.\n\
Please ensure that the correct Personal key is used..\n\n\
Select OK to continue.." 400x160
  if [ $? -eq 1 ];then
   opcanc
  else  
   echo "OK"
   # need to add code to handle 're-encrypting' back to original file..
   rm -f $input
   rm -f $input1
  fi
}
----------------------------------------------------------------------------------
As whole of the script actually works very well - thanks to various friendly script guru's from other forums who helped with my niggling "bash scripting" questions..
----------------------------------------------------------------------------------

Now where I would like to make the changes is in fact in the 'error3' code - but based on the section of the main script that calls it:

Code:
bcrypt `basename "$FILE" .bfx`.bfe < "$input" 2>/dev/null
  if [ $? -eq 1 ];then
    erpkey
    bcrypt `basename "$FILE" .bfx`.bfe < "$input1" 2>/dev/null
    if [ $? -eq 1 ];then
      error3
    else
      echo "OK"
    fi
  fi
Let me explain -

I have a file - "st-tmp1.tar.bz2.bfx.bfe" - that in the main script is selected by a 'selfile' function and presented either for encoding or for decoding -
in this case it is for 'decoding'..

In the above "Decode" section -

1. the file is first 'unencrypted' using a "master key" ("$FIL2") - the resulting filename becomes 'st-tmp1.tar.bz2.bfx'..
2. the file is then 'renamed' to - 'st-tmp1.tar.bz2.bfe' - using this code :
Code:
    wait1
    FILE=`basename "$FILE" .bfe` 
    mv "$FILE" `basename "$FILE" .bfx`.bfe
    wait1
3. this 'renamed' file is then further 'unencrypted' using a user entered "Personal Key" (via 'pkey1' function) - resulting in filename = 'st-tmp1.tar.bz2'..

If an incorrect "Personal Key" is entered - the script errors ('erpkey' function - asks for user to re-enter key) - if this 're-entered' key is still incorrect - it errors again ('error3' function) - the script now exits completely..

However it leaves a file which is not yet correctly 'unencrypted - so what I would like to do is have the 'error3' function "re-encrypt" this file back to the full original - 'st-tmp1.tar.bz2.bfx.bfe' - file, so as it then can be sent back through the main script and use the correct "Personal Key"..

So how to do this in the 'error3' function to rename from - 'st-tmp1.tar.bz2.bfe' to 'st-tmp1.tar.bz2.bfx' using the 'basename' steps - so as it then can be 're-encrypted' using the "$FIL2" master key file..

How seems to elude me..!!

The following is a test script that I worked up that does the 're-naming' perfectly as a standalone script:

Code:
#!/bin/sh 
# 
FIL1="testtmp.tmp" 
FILE="" 

function selfile { 
Xdialog --screen-center --title "Choose file to encrypt or decrypt" --fselect / 38 68 2> /root/tmp1/inputbox.tmp.$$ 
retval=$? 
FILE=`cat /root/tmp1/inputbox.tmp.$$` 
touch $FILE 
rm -f /root/tmp1/inputbox.tmp.$$ 
 if [ "$retval" -eq 0 ] 
 then 
  echo "OK" 
  Xdialog --screen-center --no-buttons --title "INFO" --infobox "Input file is '$FILE'.." 480x50 3000 >&1 
 else 
  opcanc 
 fi 
} 

echo "1" > $FIL1 
if [ ! -e "$FIL1" ];then 
  echo "Error" 
else 
  selfile 
fi 
a=`echo "$FILE" | sed "s/.*\.//"` 
if [ "$a" != "bfe" ];then 
  echo "Error" 
else 
  FILE=`basename "$FILE" .bfx`                          #this is the section 
  mv "$FILE" `basename "$FILE" .bfe`.bfx           #that does the conversion..
fi
The only problem is - the section that I know works in the above script does not want to work in the 'error3' function of the code as posted above..

So my question is :

How do I code so that I can be sure that the 'file' to be worked on is the - 'st-tmp1.tar.bz2.bfe' - named file that is the output from the
Code:
FILE=`basename "$FILE" .bfe` 
 mv "$FILE" `basename "$FILE" .bfx`.bfe
part of the script - maybe I need to reinitialize the "$FILE" variable to the new filename..?

All I want to do is properly 'rename' this generated 'st-tmp1.tar.bz2.bfe' file back to it's 1st incarnation of 'st-tmp1.tar.bz2.bfx' filename that it originated from - so as the "error" can be correctly dealt with..!!

So any help much appreciated..

TIA.

craftybytes.
.
.

Last edited by craftybytes; 02-14-2008 at 03:16 AM.
 
Old 02-13-2008, 01:50 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Hi craftybytes,

I think I got lost somewhere in the question, however my interpretation is:

You have a variable FILE=st-tmp1.tar.bz2.bfx.bfe and you want to use basename to remove the suffix of .bfx.bfe

In this case the ` symbols need to encapsulate the entire command:
Code:
`basename $FILE .bfx.bfe`

Last edited by Disillusionist; 02-13-2008 at 01:54 AM.
 
Old 02-13-2008, 02:19 AM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
The basename command will remove the directory part of the file as well as the suffix. Is that what you want?

If you only want to change the suffix from bfx to bfe you could use variable expansion:
FILE=thisisatest.bfx
mv "$FILE" "${FILE%.bfx}.bfe"
 
Old 02-13-2008, 02:28 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
Hi,

Quote:
So my question is :

How do I code so that I can be sure that the 'file' to be worked on is the - 'st-tmp1.tar.bz2.bfe' - named file that is the output from the
Code:
FILE=`basename "$FILE" .bfe` 
 mv "$FILE" `basename "$FILE" .bfx`.bfe
part of the script - maybe I need to reinitialize the "$FILE" variable to the new filename..?

All I want to do is properly 'rename' this generated 'st-tmp1.tar.bz2.bfe' file back to it's 1st incarnation of 'st-tmp1.tar.bz2.bfx' filename that it originated from - so as the "error" can be correctly dealt with..!!
Why use basename (an external program)?

You could try to use a bash internal. I.e:

mv st-tmp1.tar.bz2.bfe ${FILE%%.bfe}.bfx

The ${FILE%%.bfe} part takes what $FILE holds and 'deletes' the .bfe part (%%.bfe), the .bfx part appends after the ${....} part (see bash's manpage for details).

Here's an example:
Quote:
$ touch st-tmp1.tar.bz2.bfe

$ ls -l st*
-rw-rw-r-- 1 jde jde 0 Feb 13 09:22 st-tmp1.tar.bz2.bfe

$ mv st-tmp1.tar.bz2.bfe ${FILE%%.bfe}.bfx

$ ls -l st*
-rw-rw-r-- 1 jde jde 0 Feb 13 09:22 st-tmp1.tar.bz2.bfx

Hope this helps.
 
Old 02-13-2008, 10:51 PM   #5
craftybytes
Member
 
Registered: Jan 2006
Location: Killarney, QLD Australia
Distribution: Mepis 3.4-3; Puppy 2.12; WinXP Pro (rarely)
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks guys for the responses..
--------------------------------------------------------------------------
@disillusionist -

Yeh, I realize that my post was a little on the long side..
Code:
You have a variable FILE=st-tmp1.tar.bz2.bfx.bfe and you want to use basename to remove the suffix of .bfx.bfe
No - the filename at the part in my script that the error function is trying to cover is actually - 'st-tmp1.tar.bz2.bfe' - it has already been renamed earlier in the script using the "mv" command from - 'st-tmp1.tar.bz2.bfx' - and this is correct and all works well..

@jschiwal -

Yes & no..

Code:
FILE=thisisatest.bfx
mv "$FILE" "${FILE%.bfx}.bfe"
does work fine as standalone - as I explained in my post - my 'test' script does the same..

But 'FILE=thisisatest.bfx' filename is OK as standalone - BUT - my main script does not know what the "actual" filename is going to be - so it uses a variable "$FILE" which is determined by using the "basename" & "mv" commands to get the originally selected filename (via the 'selfile' function) down to the relevant name parts to be used, e,g:
Code:
FILE=`basename "$FILE" .bfe` 
mv "$FILE" `basename "$FILE" .bfx`.bfe
and this works well..

@druuna -

Yep - would be nice if one could use a bash internal like you pointed out - but - see my reply to 'jschiwal' above..
---------------------------------------------------------------------------------

For the 'error3' function code to work - all that is required is to "rename" the working file from '$FILE.bfe' back to '$FILE.bfx' - but - as explained in my first post the 'inverse' to :
Code:
FILE=`basename "$FILE" .bfe` 
mv "$FILE" `basename "$FILE" .bfx`.bfe
e.g..:
Code:
FILE=`basename "$FILE" .bfx` 
mv "$FILE" `basename "$FILE" .bfe`.bfx
for some unseen reason just does not work..
It errors -
Code:
mv: cannot stat `st-tmp1.tar.bz2.bfe`: No such file or directory
BTW - have tried all your various suggestions - still no luck as yet..

Thanks for the help anyways..

crafty.
.
 
Old 02-13-2008, 11:07 PM   #6
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
We weren't trying to say you had to hardwire the name of the command. We were just showing how to change the suffix. It seemed that you were trying to change just the suffix. The basename command will change the suffix and remove any directory portion of the file. Instead of posting your entire program, post examples of what $FILE might contain before & after.
 
Old 02-14-2008, 02:35 AM   #7
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
You seem to be using basename to modify the value of $FILE before you run the mv command.

Code:
FILE=`basename "$FILE" .bfe` 
mv "$FILE" `basename "$FILE" .bfx`.bfe
As per jschiwal's post we need to know the answer to a couple of questions.

Question 1: What was the value of $FILE before the first line in the extract above?

Question 2: What was the value of $FILE after the first line.

Question 3: Does the file exist in the current working directory?

Question 4: Is this the file you want to move? (see example)


Example:
Code:
## FILE currently set to "my-test-file.bfe"

FILE=`basename "$FILE" .bfe`
## FILE now set to "my-test-file"

mv "$FILE" `basename "FILE" .bfx`.bfe
##Basically: mv my-test-file my-test-file.bfe
If my-test-file doesn't exists in the current working directory, the move command will fail.

If my-test-file.bfe exists in the current working directory, it will be overwritten with my-test-file

As stated by druuna and jschiwal if $FILE is set to "/path/to/my-test-file.bfe" the use of basename will strip the path leaving: "my-test-file"

Additionally, instead of using the ` symbols when running a nested command like that you may want to use:

mv "$FILE" $(basename "$FILE" .bfx).bfe

Last edited by Disillusionist; 02-14-2008 at 02:36 AM.
 
Old 02-14-2008, 03:05 AM   #8
craftybytes
Member
 
Registered: Jan 2006
Location: Killarney, QLD Australia
Distribution: Mepis 3.4-3; Puppy 2.12; WinXP Pro (rarely)
Posts: 54

Original Poster
Rep: Reputation: 15
Hi guys..

Yep - you both be right AND I was also right in the last part of my question in my 1st post..!!

With your excellent help I've managed to solve the problem..

1. Had to add a couple of extra steps in the main part of the script :

Code:
FILE=`basename "$FILE" .bfe`
mv "$FILE" `basename "$FILE" .bfx`.bfe
FILE=`basename "$FILE" .bfx`.bfe          #added to reset 'FILE' variable
touch $FILE                                              #added to ensure $FILE is file accessed..
2. Then modified the 'error3' function to :
Code:
function error3 {
Xdialog --screen-center --title "ERROR" --msgbox "Still incorrect personal encryption key.\n\
Please ensure that the correct Personal key is used..\n\
Please use '007' program to finish decoding..!\n\n
Select OK to continue.." 400x160
  if [ $? -eq 1 ];then
   opcanc
  else  
   echo "OK"
   mv "$FILE" "${FILE%.bfe}.bfx"        #do this to rename '.bfe' file to '.bfx' file..
   FILE="${FILE%.bfe}.bfx"                #do this to reset 'FILE' variable..
   touch $FILE                                    #do this to ensure '$FILE' is file accessed..
   wait1
   bcrypt $FILE < "$FIL2" 2>/dev/null
   Xdialog --screen-center --wrap --no-buttons --title "INFO" --infobox "Encoding completed.!" 180x50 2000 >&1
   rm -f $input
   rm -f $input1
  fi
}
Now it all works as I intended - GREAT..

I also learnt a lot re bash & Linux scripting from this problem..

Thanks muchly for all your valuable assistance..

craftybytes.
.
.
 
Old 02-14-2008, 03:07 AM   #9
craftybytes
Member
 
Registered: Jan 2006
Location: Killarney, QLD Australia
Distribution: Mepis 3.4-3; Puppy 2.12; WinXP Pro (rarely)
Posts: 54

Original Poster
Rep: Reputation: 15
Hi guys..

Yep - you both be right AND I was also right in the last part of my question in my 1st post..!!

With your excellent help I've managed to solve the problem..

1. Had to add a couple of extra steps in the main part of the script :

Code:
FILE=`basename "$FILE" .bfe`
mv "$FILE" `basename "$FILE" .bfx`.bfe
FILE=`basename "$FILE" .bfx`.bfe          #added to reset 'FILE' variable
touch $FILE                                 #added to ensure $FILE is file accessed..
2. Then modified the 'error3' function to :
Code:
function error3 {
Xdialog --screen-center --title "ERROR" --msgbox "Still incorrect personal encryption key.\n\
Please ensure that the correct Personal key is used..\n\n\
Select OK to continue.." 400x160
  if [ $? -eq 1 ];then
   opcanc
  else  
   echo "OK"
   mv "$FILE" "${FILE%.bfe}.bfx"        #do this to rename '.bfe' file to '.bfx' file..
   FILE="${FILE%.bfe}.bfx"                #do this to reset 'FILE' variable..
   touch $FILE                                    #do this to ensure '$FILE' is file accessed..
   wait1
   bcrypt $FILE < "$FIL2" 2>/dev/null
   Xdialog --screen-center --wrap --no-buttons --title "INFO" --infobox "Encoding completed.!" 180x50 2000 >&1
   rm -f $input
   rm -f $input1
  fi
}
Now it all works as I intended - GREAT..

I also learnt a lot re bash & Linux scripting from this problem..

Thanks muchly for all your valuable assistance..

craftybytes.
.
.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the 'basename' (or what other.?) command to do what I want..? craftybytes Programming 1 02-13-2008 06:27 AM
Bash Script as command whited Programming 15 12-10-2007 06:14 AM
dirname basename; how to capture extensions from bash? carl0ski Linux - General 2 07-22-2005 07:26 AM
Simple Bash Script Help Command kemplej Linux - Software 1 03-11-2004 03:52 AM
BASH scripting problem, spaces in filenames / using basename textures Programming 24 11-16-2003 01:41 AM


All times are GMT -5. The time now is 10:58 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration