Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

For example:

7za e -y $file_name -o$dest_directory

This works fine, when run directly from command line, but within the script it gives this error:

could not find archive (No archive by that name).

I have tried using:

7za e -y `ls -a | grep 001` -o`pwd` 

This extracts the files successfully but the files are nowhere to be found, when the script ends.

share|improve this question
    
Can you define the problem a bit more clearly - where are you sourcing/setting $file_name and $dest_directory ? What do you intend 7za e -y l a | grep --1 -o pwd to do ? (Looks to me like there might be some backticks missing - I wonder if SU culled them ?) –  davidgo Feb 2 '13 at 6:58
    
I wonder if your problem could be parsing $file_name and $dest_directory to the script. I suspect that file_name is not exported to subshells. (To do this you would need to type export file_name; export dest_directory BEFORE running the script) –  davidgo Feb 2 '13 at 7:04
    
@david go: you are right, i forgot to type the back-ticks in my problem statement. Below is the script I have modified to. However, it extracts the files in directory - ./? . How do I resolve this? !/bin/bash; printf "%s\n" "Please enter full path to the vmfile location e.g. /path/to - " ; read ; vmfile_path=$REPLY ; cd $vmfile_path; 7za e -y ls -a | grep 001 -opwd; –  Bhavya Maheshwari Feb 2 '13 at 11:26

1 Answer 1

up vote 2 down vote accepted

I copy+paste your script from the comment above here with line breaks added at the ; signs for readability:

!/bin/bash
printf "%s\n" "Please enter full path to the vmfile location e.g. /path/to - "
read
vmfile_path=$REPLY
cd $vmfile_path
7za e -y ls -a | grep 001 -o`pwd`
  • To begin with, the first line should be #!/bin/bash if anything, but I guess that is a typo.

  • Your read command and the subsequent line are easier written as just read vmfile_path. This way the shebang could also actually be changed to just #!/bin/sh since you won't be using any Bash specific functions.

  • You should quote the cd argument to handle directory names with spaces.

  • I guess that you want to extract a file whose name contains the string 001 in the last row. Right now it is lacking some process substitution, and your intention is probably closer to:

    7za e -y $(ls -a | grep 001) -o$(pwd)
    

    The $() syntax is easier to read (and has some other advantages) compared to the backticks, but it does the same thing.

    I would also guess that the output directory defaults to the pwd output, making it superfluous, but I don't know for sure.

  • Your command will not like it if there are more than one file name matching 001.

Ignoring the last problem, you would in total want something like:

#!/bin/sh
printf '%s\n' "Please enter full path to the vmfile location e.g. /path/to - "
read vmfile_path
cd "$vmfile_path"
7za e -y "$(ls -a | grep 001)" -o"$(pwd)"

Or, simpler, replace the last two lines with (will also handle multiple matches) (though it is untested):

find "$vmfile_path" -maxdepth 1 -name '*001*' -exec 7za e -y -o"$vmfile_path" {} \;

The name matching pattern could probably be improved if you know more of the filename.

share|improve this answer
    
thanks a lot. i was getting the problems because I had written the script in Text editor on windows and the scp'ed to server and was running it. The other inefficiencies in the script were also to circumvent the problem being caused by the same, such as printf , read and then var = read's input. I rewrote the whole script using vi editor and everything is working fine now. Thanks a lot though. –  Bhavya Maheshwari Feb 4 '13 at 8:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.