Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
Hello everybody, I'm new to Linux and to this forum so bear with me if you find my question to be so simple, need help and I can't figure this ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2013
    Posts
    8

    Pass filename to a variable, shell script


    Hello everybody, I'm new to Linux and to this forum so bear with me if you find my question to be so simple, need help and I can't figure this out, I guess for you will be a piece of cake.

    I'm making an automatic backup of a Asterisk system, the resulting file name is a complex name so it's not a fixed filename. I am able to retrieve the file name with the following command:
    "find *.tar -type f -mtime -1", better yet, "find /var/www/backup/*.tar -type f -mtime -1".

    What I'm trying to do on a script is pass the result of that command into a variable which I can read later on the same script and transfer file to a FTP server that I have already set up.

    How do I run a command and pass the result to a variable? (that is get the filename into a variable that I can read using $FILE, for example). I guess it would be a simple thing to do but for some reason I can't get to it.

    Thanks in advance.

  2. #2
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    673
    There is an example for you to play with and get it to do what you want.

    Code:
    # creating an array called "tar_file_list"
    tar_file_list=( $(find /var/www/backup/*.tar -type f -mtime -1) )
    # access the length of the array
    echo There are "${#tar_file_list[@]}" files found.
    # direct usage within loop
    for tar_file in "${tar_file_list[@]}"; do
        echo "${tar_file}"
    done
    # using indexing:
    echo The fist name in the list is: "${tar_file_list[0]}"
    echo The second name in the list is: "${tar_file_list[1]}"
    # using a loop of the indexes for the array
    for idx in "${!tar_file_list[@]}"; do
        echo "${tar_file_list[${idx}]}"
    done

  3. #3
    Just Joined!
    Join Date
    Dec 2013
    Posts
    8
    Thanks for your help. I will test this out later today and reply with the results.

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Dec 2013
    Posts
    8
    I was playing with this and every time I do echo to display the result I get the full path of the file instead just the file name, this is what I get:

    "/var/www/backup/elastixbackup-20131202135310-r3.tar"

    I also get:

    There are 1 files found.
    /var/www/backup/elastixbackup-20131202135310-r3.tar
    The fist name in the list is: /var/www/backup/elastixbackup-20131202135310-r3.tar
    The second name in the list is:
    /var/www/backup/elastixbackup-20131202135310-r3.tar

    Was playing with this but I get the full path every time, how do I make to just get the file name without the path? this is the main thing I'm looking for.

    Thanks for bearing with me

  6. #5
    Just Joined!
    Join Date
    Dec 2013
    Posts
    8
    Doing some more tweaking I was able to get what I wanted, used the following instruction:

    File=($(find *.tar -type f -mtime -4)), the trick here will be invoking the script from /var/www/backup and not from any other path. I am now closer to what I want to achieve.

    Thanks for your patience with this newbie

  7. #6
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    237
    hi,

    Code:
    find /var/www/backup -type f -iname "*.tar" -mtime -4 -printf '%f\n'
    your algorithm seems weak; why store filename in a variable to be used later?

    storing the output of a command by expanding it into an array is as bad as using a for loop to parse it : filenames may have weird characters that may break the array.

    if you want to build an array from find's output having only filename (not directoy) use parameter expansion:
    Code:
    while IFS='' read -d '' f
    do
       array+=( "${f##*/}" )
    done < <(find ~ -type f -print0)
    note that I simplified the find command so there's not too much items, you'll have to adapt to your needs.

  8. #7
    Just Joined!
    Join Date
    Dec 2013
    Posts
    8
    Thanks for your 2 cents. your first code works like a charm and can be run from any other location, will use is for the final script. When I stated the use of the variable for later I meant to use its value since in the same script I will upload the resulting file to a FTP server that it is already set up for this purpose using the mput command (mput $File). Thanks for your help.

  9. #8
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    673
    Change:
    Code:
    tar_file_list=( $(find /var/www/backup/*.tar -type f -mtime -1) )
    To be:
    Code:
    from_where="/var/www/backup/"
    tar_file_list=( $(find ${from_where} -name '*.tar ' -type f -mtime -1) )
    The first argument to find is the path to start looking for the files. My error as I grabbed your preferred "find" command without looking at it.
    Last edited by alf55; 12-07-2013 at 06:00 PM.

  10. #9
    Linux Enthusiast
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    673
    You do not need a loop to make the array when using the output of a command. This builds the array without the loop:
    Code:
    starting_dir_location="/var/www/backup/" # or where ever
    filenames=(  $(find ${starting_dir_location} -type f -iname "*.tar" -mtime -4 -print) )

  11. #10
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    237
    of course you need:
    Code:
    $ ls -1
    myFile
    my File
    $ ar=( $(find ./ -type f) )
    $ printf '%s\n' "${ar[@]}"
    ./my
    File
    ./myFile
    $ unset ar
    $ while IFS= read -d '' -r f; do ar+=( "$f" ); done < <(find ./ -type f -print0)
    $ printf '%s\n' "${ar[@]}"
    ./my File
    ./myFile
    !

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •