Find the answer to your Linux question:
Results 1 to 6 of 6
Hi folks, I have a bash script with following line running at its end ......... ...... # Remove ISO File... rm -i $ISO_File - End of Script - The script ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,807

    Bash script - to save and run a command at the end


    Hi folks,

    I have a bash script with following line running at its end
    .........
    ......
    # Remove ISO File...
    rm -i $ISO_File
    - End of Script -

    The script is working without problem.

    Now I expect to set this line as argument, i.e. removing it leaving only there;
    ........
    .......
    # Remove ISO File ...
    (Empty)
    - End of Script -

    Somewhere at the beginning of the Script add following lines
    .......
    read -p "Remove ISO File <yes|no>:"
    if [ "$REPLY" = "yes" ]; then
    echo "rm $ISO_File"
    read
    etc.

    I have no idea how to get the line "rm $ISO_File" saved after
    # Remove ISO File...

    and run at the end of the script

    Please advise. TIA

    B.R.
    satimis

  2. #2
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Move the comment?

    I'm not sure I understand what exactly you are trying to accomplish. Where is the script getting the ISO name? What are you trying to accomplish? What does the rest of the script do?
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  3. #3
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,807
    Hi sarumont,

    I'm not sure I understand what exactly you are trying to accomplish. Where is the script getting the ISO name? What are you trying to accomplish? What does the rest of the script do?
    Hereinunder is the full script
    #!/bin/bash

    #Set directory path...
    user=$(whoami)
    now=$(date +%Y.%m.%d.%R)
    ISO_File="/tmp/satimis/image_${use* r}_${now}.iso"
    Default=$1

    #Temporary file to store the resulting mkisofs command
    CmdFile_1=/tmp/satimis/.TmpBurnCD$* $
    list=""

    CopyDir(){
    name=$1
    dir=$2

    if [ "$Default" = "" ];then
    list="$list \"$name=$dir\""
    else
    if [ "$Default" = "yes" ];then
    list="$list \"$name=$dir\""
    fi
    fi
    }

    CopyDir "/Document/" "/home/satimis/Document/"
    CopyDir "/Photo/" "/home/satimis/Photo/"

    # Loop to get other directories...
    echo "Enter directories to burn. Leave blank to end list"
    entry="empty"

    until [ -z $entry ]; do
    echo -n "Enter a directory (eg: /Photo/=/home/satimis/Photo): "
    read entry
    if [ "$entry" != "" ]; then
    # OR USE if [ z$entry != "z" ]; then
    list="$list \"$entry\""
    fi
    done

    # If at least one directory has been entered....
    if [ "$list" != "" ];then
    #Construct the mkisofs command...
    echo "/usr/bin/mkisofs -R -l -o $ISO_File -hide-rr-moved -graft-points \
    $list" > "$CmdFile_1"

    #Now create the image
    /bin/sh "$CmdFile_1"

    #Remove the temporary file
    rm -f "$CmdFile_1"

    #Temporary file to run cdrecord command
    CmdFile_2=/tmp/satimis/.TmpCdrecor* d$$
    su -c "cdrecord dev=ATA:1,0,0 -v -eject $ISO_File"

    #Now run cdrecord
    /bin/sh "$CmdFile_2"

    #Remove the temporary file
    rm -f "$CmdFile_2"
    fi

    #Remove ISO File...
    rm -i $ISO_File
    Name of script = cdmaker

    The script copies files from preset and input directories, burning them on CD and remove the ISO File finally. Before removing the ISO File, option prompts to keep it or remove it.

    Now I want to make this option to be selected at the beginning of the script but the command runs at end of the script after CD burnt.

    Code:
    ......
    read -p "Remove ISO File <yes|no>&#58;"
    if &#91; "$REPLY" = "yes" &#93;; then
    echo "rm $ISO_File" >> cdmaker
    may achieve my goal. But "rm $ISO_File" will be added permanently to the script.

    I'm now looking for a solution adding "rm $ISO_File" temporarily at end of the script only on being selected.

    B.R.
    satimis

  4. #4
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Just put a prompt at the beginning of the script to ask to keep the ISO. Then at the end, check the value of that variable (choose a name that you're not using elsewhere) and deal with it accordingly.

    Code:
    readline -p "Delete ISO when done &#40;yes/no&#41;? " REPLY
    #Input integrity checking
    
    ...rest of script...
    
    if&#91; $REPLY = yes &#93;; then 
         rm $ISO_File
    fi
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

  5. #5
    Linux Guru
    Join Date
    Sep 2004
    Posts
    1,807
    Hi sarumont,

    Tks for your advice.

    Just put a prompt at the beginning of the script to ask to keep the ISO. Then at the end, check the value of that variable (choose a name that you're not using elsewhere) and deal with it accordingly.
    .......
    Directory for keeping ISO file already existing

    /tmp/satimis/

    Test performed as follow;

    "readline -p "Delete ISO when done <yes|no>?" REPLY" won't ask for input. I changed it as "read -p "Delete ISO when done <yes|no>?" REPLY" Then if asked for input

    Script:-
    Code:
    #!/bin/bash
    set -x
    
    #Set directory path...
    user=$&#40;whoami&#41;
    now=$&#40;date +%Y.%m.%d.%R&#41;
    ISO_File="/tmp/satimis/image_$&#123;user&#125;_$&#123;now&#125;.iso"
    
    # Remove ISO File...
    read -p "Delete ISO when done <yes|no>?" REPLY
    #Input integrity checking
    .....
    # My script
    .....
    # Remove ISO File...
    if &#91; $REPLY = yes &#93;; then
         rm $ISO_File
    fi
    Printout :-
    Code:
    .......
    + rm -i /tmp/satimis/image_satimis_2004.11.02.16&#58;13.iso
    rm&#58; remove regular file `/tmp/satimis/image_satimis_2004.11.02.16&#58;13.iso'? y
    + '&#91;' '' = yes '&#93;'
    The script asked for input <yes|no>? at the begining. It asked again at the end, twice/2nd time,

    I think it is the
    rm
    is often aliased to
    rm -i
    "rm' removed the ISO file not your additional script

    B.R.
    satimiks

  6. #6
    Linux Guru sarumont's Avatar
    Join Date
    Apr 2003
    Location
    /dev/urandom
    Posts
    3,682
    Oops...forgot that it was read and not readline.

    As to the rm thing...bypass the alias using:

    Code:
    command rm $ISO_File
    "Time is an illusion. Lunchtime, doubly so."
    ~Douglas Adams, The Hitchhiker's Guide to the Galaxy

Posting Permissions

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