correct this script

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
-3
down vote

favorite












I am having a problem with this script



#!/bin/bash
echo “Enter option “
echo “1 PIC”
echo “2 ICON”
read $answer
echo

if [[$answer == 1 ]] ; then
rename 's/^.*([0-9]3.jpg)$/ICON$1/' *.jpg ;
echo “done”

elif
if [[$answer == 2 ]] ; then
rename 's/^.*([0-9]3.jpg)$/PIC$1/' *.jpg ;
echo “done”
else
echo “Error”
fi
done









share|improve this question









New contributor




Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Give more info: what you want, what are the results. Or your question will be closed!
    – peterh
    2 hours ago










  • It is a script that renames images in a file to either PIC or ICON with an option menu with it . Wen ever I run the script it keeps telling me error
    – Suh Fru Kieran Claver
    2 hours ago






  • 2




    Off-topic (typo): [[ must be followed by a space. Double quotes should be " not typographical double quotes (“...”). When using read the variable name should be used without $. Also, you don't mention what errors you are getting. See shellcheck.net
    – Kusalananda
    2 hours ago











  • The script doesn’t execute and it skips down to the last part were it says echo Error
    – Suh Fru Kieran Claver
    2 hours ago










  • The original version of your script was so buggy, that I couldn't even correctly indent it. Its current version might be a better option for bugfixing.
    – peterh
    2 hours ago














up vote
-3
down vote

favorite












I am having a problem with this script



#!/bin/bash
echo “Enter option “
echo “1 PIC”
echo “2 ICON”
read $answer
echo

if [[$answer == 1 ]] ; then
rename 's/^.*([0-9]3.jpg)$/ICON$1/' *.jpg ;
echo “done”

elif
if [[$answer == 2 ]] ; then
rename 's/^.*([0-9]3.jpg)$/PIC$1/' *.jpg ;
echo “done”
else
echo “Error”
fi
done









share|improve this question









New contributor




Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Give more info: what you want, what are the results. Or your question will be closed!
    – peterh
    2 hours ago










  • It is a script that renames images in a file to either PIC or ICON with an option menu with it . Wen ever I run the script it keeps telling me error
    – Suh Fru Kieran Claver
    2 hours ago






  • 2




    Off-topic (typo): [[ must be followed by a space. Double quotes should be " not typographical double quotes (“...”). When using read the variable name should be used without $. Also, you don't mention what errors you are getting. See shellcheck.net
    – Kusalananda
    2 hours ago











  • The script doesn’t execute and it skips down to the last part were it says echo Error
    – Suh Fru Kieran Claver
    2 hours ago










  • The original version of your script was so buggy, that I couldn't even correctly indent it. Its current version might be a better option for bugfixing.
    – peterh
    2 hours ago












up vote
-3
down vote

favorite









up vote
-3
down vote

favorite











I am having a problem with this script



#!/bin/bash
echo “Enter option “
echo “1 PIC”
echo “2 ICON”
read $answer
echo

if [[$answer == 1 ]] ; then
rename 's/^.*([0-9]3.jpg)$/ICON$1/' *.jpg ;
echo “done”

elif
if [[$answer == 2 ]] ; then
rename 's/^.*([0-9]3.jpg)$/PIC$1/' *.jpg ;
echo “done”
else
echo “Error”
fi
done









share|improve this question









New contributor




Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I am having a problem with this script



#!/bin/bash
echo “Enter option “
echo “1 PIC”
echo “2 ICON”
read $answer
echo

if [[$answer == 1 ]] ; then
rename 's/^.*([0-9]3.jpg)$/ICON$1/' *.jpg ;
echo “done”

elif
if [[$answer == 2 ]] ; then
rename 's/^.*([0-9]3.jpg)$/PIC$1/' *.jpg ;
echo “done”
else
echo “Error”
fi
done






bash shell-script






share|improve this question









New contributor




Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 mins ago









Rui F Ribeiro

37.6k1475119




37.6k1475119






New contributor




Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Suh Fru Kieran Claver

11




11




New contributor




Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Suh Fru Kieran Claver is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Give more info: what you want, what are the results. Or your question will be closed!
    – peterh
    2 hours ago










  • It is a script that renames images in a file to either PIC or ICON with an option menu with it . Wen ever I run the script it keeps telling me error
    – Suh Fru Kieran Claver
    2 hours ago






  • 2




    Off-topic (typo): [[ must be followed by a space. Double quotes should be " not typographical double quotes (“...”). When using read the variable name should be used without $. Also, you don't mention what errors you are getting. See shellcheck.net
    – Kusalananda
    2 hours ago











  • The script doesn’t execute and it skips down to the last part were it says echo Error
    – Suh Fru Kieran Claver
    2 hours ago










  • The original version of your script was so buggy, that I couldn't even correctly indent it. Its current version might be a better option for bugfixing.
    – peterh
    2 hours ago
















  • Give more info: what you want, what are the results. Or your question will be closed!
    – peterh
    2 hours ago










  • It is a script that renames images in a file to either PIC or ICON with an option menu with it . Wen ever I run the script it keeps telling me error
    – Suh Fru Kieran Claver
    2 hours ago






  • 2




    Off-topic (typo): [[ must be followed by a space. Double quotes should be " not typographical double quotes (“...”). When using read the variable name should be used without $. Also, you don't mention what errors you are getting. See shellcheck.net
    – Kusalananda
    2 hours ago











  • The script doesn’t execute and it skips down to the last part were it says echo Error
    – Suh Fru Kieran Claver
    2 hours ago










  • The original version of your script was so buggy, that I couldn't even correctly indent it. Its current version might be a better option for bugfixing.
    – peterh
    2 hours ago















Give more info: what you want, what are the results. Or your question will be closed!
– peterh
2 hours ago




Give more info: what you want, what are the results. Or your question will be closed!
– peterh
2 hours ago












It is a script that renames images in a file to either PIC or ICON with an option menu with it . Wen ever I run the script it keeps telling me error
– Suh Fru Kieran Claver
2 hours ago




It is a script that renames images in a file to either PIC or ICON with an option menu with it . Wen ever I run the script it keeps telling me error
– Suh Fru Kieran Claver
2 hours ago




2




2




Off-topic (typo): [[ must be followed by a space. Double quotes should be " not typographical double quotes (“...”). When using read the variable name should be used without $. Also, you don't mention what errors you are getting. See shellcheck.net
– Kusalananda
2 hours ago





Off-topic (typo): [[ must be followed by a space. Double quotes should be " not typographical double quotes (“...”). When using read the variable name should be used without $. Also, you don't mention what errors you are getting. See shellcheck.net
– Kusalananda
2 hours ago













The script doesn’t execute and it skips down to the last part were it says echo Error
– Suh Fru Kieran Claver
2 hours ago




The script doesn’t execute and it skips down to the last part were it says echo Error
– Suh Fru Kieran Claver
2 hours ago












The original version of your script was so buggy, that I couldn't even correctly indent it. Its current version might be a better option for bugfixing.
– peterh
2 hours ago




The original version of your script was so buggy, that I couldn't even correctly indent it. Its current version might be a better option for bugfixing.
– peterh
2 hours ago










2 Answers
2






active

oldest

votes

















up vote
0
down vote













I think you want this. The list of the problems in your original script would be longer than the script itself.



#!/bin/bash
echo "Enter option"
echo "1 PIC"
echo "2 ICON"
read answer
echo

if [ "$answer" == 1 ]
then
ext=ICON
elif [ "$answer" == 2 ]
then
ext=PIC
else
echo "Error"
exit 1
fi

ls|grep -i .jpg$|while read i
do
mv -vf "$i" "$i%.*"."$ext"
done

echo "done"





share|improve this answer




















  • I don't think the original script replaces the suffix, looks more like it renames whatever123.jpg to PIC123.jpg.
    – nohillside
    1 hour ago


















up vote
0
down vote













To replace all but the last three digits and the filename suffix of all JPEG files in the current directory with either the word PIC or ICON, you may do the following (this interactively asks for the prefix to use):



#!/bin/bash

echo 'Please select prefix:' >&2

select prefix in 'ICON' 'PIC'; do
case $REPLY in
1) break ;;
2) break ;;
*) echo 'Try again' >&2
esac
done

rename 's/.*([0-9][0-9][0-9].jpg)/'"$prefix"'$1/' *[0-9][0-9][0-9].jpg

echo 'Done' >&2


I've chosen to use select as this produces a nice looking menu of the available options. I'm also applying the rename command only to the files that I know can be renamed (there may be other JPEG files in the directory that does not have the three digits, for example). All output from this script is strictly user interaction, so I'm doing this on the standard error stream (with >&2).




The issues with your original script are




  1. [[ must be followed by a space.

  2. Double quotes are "...", not “...”.

  3. The read utility takes a variable name, not a value.

  4. There is a dangling done at the end of the code.


  5. elif expects a test (not an if).

A suggestion would be to run you scripts through a syntax checker, such as the one available online at https://www.shellcheck.net/



Also, when asking questions like these, it is quite important to include the exact error message produced by the script, and also to mention what it is that you actually want to do (so that we don't have to reverse-engineer your code). See e.g. Jeff's excellent Meta question-and-answer about asking well received questions.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    Suh Fru Kieran Claver is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478095%2fcorrect-this-script%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I think you want this. The list of the problems in your original script would be longer than the script itself.



    #!/bin/bash
    echo "Enter option"
    echo "1 PIC"
    echo "2 ICON"
    read answer
    echo

    if [ "$answer" == 1 ]
    then
    ext=ICON
    elif [ "$answer" == 2 ]
    then
    ext=PIC
    else
    echo "Error"
    exit 1
    fi

    ls|grep -i .jpg$|while read i
    do
    mv -vf "$i" "$i%.*"."$ext"
    done

    echo "done"





    share|improve this answer




















    • I don't think the original script replaces the suffix, looks more like it renames whatever123.jpg to PIC123.jpg.
      – nohillside
      1 hour ago















    up vote
    0
    down vote













    I think you want this. The list of the problems in your original script would be longer than the script itself.



    #!/bin/bash
    echo "Enter option"
    echo "1 PIC"
    echo "2 ICON"
    read answer
    echo

    if [ "$answer" == 1 ]
    then
    ext=ICON
    elif [ "$answer" == 2 ]
    then
    ext=PIC
    else
    echo "Error"
    exit 1
    fi

    ls|grep -i .jpg$|while read i
    do
    mv -vf "$i" "$i%.*"."$ext"
    done

    echo "done"





    share|improve this answer




















    • I don't think the original script replaces the suffix, looks more like it renames whatever123.jpg to PIC123.jpg.
      – nohillside
      1 hour ago













    up vote
    0
    down vote










    up vote
    0
    down vote









    I think you want this. The list of the problems in your original script would be longer than the script itself.



    #!/bin/bash
    echo "Enter option"
    echo "1 PIC"
    echo "2 ICON"
    read answer
    echo

    if [ "$answer" == 1 ]
    then
    ext=ICON
    elif [ "$answer" == 2 ]
    then
    ext=PIC
    else
    echo "Error"
    exit 1
    fi

    ls|grep -i .jpg$|while read i
    do
    mv -vf "$i" "$i%.*"."$ext"
    done

    echo "done"





    share|improve this answer












    I think you want this. The list of the problems in your original script would be longer than the script itself.



    #!/bin/bash
    echo "Enter option"
    echo "1 PIC"
    echo "2 ICON"
    read answer
    echo

    if [ "$answer" == 1 ]
    then
    ext=ICON
    elif [ "$answer" == 2 ]
    then
    ext=PIC
    else
    echo "Error"
    exit 1
    fi

    ls|grep -i .jpg$|while read i
    do
    mv -vf "$i" "$i%.*"."$ext"
    done

    echo "done"






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 2 hours ago









    peterh

    4,07092856




    4,07092856











    • I don't think the original script replaces the suffix, looks more like it renames whatever123.jpg to PIC123.jpg.
      – nohillside
      1 hour ago

















    • I don't think the original script replaces the suffix, looks more like it renames whatever123.jpg to PIC123.jpg.
      – nohillside
      1 hour ago
















    I don't think the original script replaces the suffix, looks more like it renames whatever123.jpg to PIC123.jpg.
    – nohillside
    1 hour ago





    I don't think the original script replaces the suffix, looks more like it renames whatever123.jpg to PIC123.jpg.
    – nohillside
    1 hour ago













    up vote
    0
    down vote













    To replace all but the last three digits and the filename suffix of all JPEG files in the current directory with either the word PIC or ICON, you may do the following (this interactively asks for the prefix to use):



    #!/bin/bash

    echo 'Please select prefix:' >&2

    select prefix in 'ICON' 'PIC'; do
    case $REPLY in
    1) break ;;
    2) break ;;
    *) echo 'Try again' >&2
    esac
    done

    rename 's/.*([0-9][0-9][0-9].jpg)/'"$prefix"'$1/' *[0-9][0-9][0-9].jpg

    echo 'Done' >&2


    I've chosen to use select as this produces a nice looking menu of the available options. I'm also applying the rename command only to the files that I know can be renamed (there may be other JPEG files in the directory that does not have the three digits, for example). All output from this script is strictly user interaction, so I'm doing this on the standard error stream (with >&2).




    The issues with your original script are




    1. [[ must be followed by a space.

    2. Double quotes are "...", not “...”.

    3. The read utility takes a variable name, not a value.

    4. There is a dangling done at the end of the code.


    5. elif expects a test (not an if).

    A suggestion would be to run you scripts through a syntax checker, such as the one available online at https://www.shellcheck.net/



    Also, when asking questions like these, it is quite important to include the exact error message produced by the script, and also to mention what it is that you actually want to do (so that we don't have to reverse-engineer your code). See e.g. Jeff's excellent Meta question-and-answer about asking well received questions.






    share|improve this answer


























      up vote
      0
      down vote













      To replace all but the last three digits and the filename suffix of all JPEG files in the current directory with either the word PIC or ICON, you may do the following (this interactively asks for the prefix to use):



      #!/bin/bash

      echo 'Please select prefix:' >&2

      select prefix in 'ICON' 'PIC'; do
      case $REPLY in
      1) break ;;
      2) break ;;
      *) echo 'Try again' >&2
      esac
      done

      rename 's/.*([0-9][0-9][0-9].jpg)/'"$prefix"'$1/' *[0-9][0-9][0-9].jpg

      echo 'Done' >&2


      I've chosen to use select as this produces a nice looking menu of the available options. I'm also applying the rename command only to the files that I know can be renamed (there may be other JPEG files in the directory that does not have the three digits, for example). All output from this script is strictly user interaction, so I'm doing this on the standard error stream (with >&2).




      The issues with your original script are




      1. [[ must be followed by a space.

      2. Double quotes are "...", not “...”.

      3. The read utility takes a variable name, not a value.

      4. There is a dangling done at the end of the code.


      5. elif expects a test (not an if).

      A suggestion would be to run you scripts through a syntax checker, such as the one available online at https://www.shellcheck.net/



      Also, when asking questions like these, it is quite important to include the exact error message produced by the script, and also to mention what it is that you actually want to do (so that we don't have to reverse-engineer your code). See e.g. Jeff's excellent Meta question-and-answer about asking well received questions.






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        To replace all but the last three digits and the filename suffix of all JPEG files in the current directory with either the word PIC or ICON, you may do the following (this interactively asks for the prefix to use):



        #!/bin/bash

        echo 'Please select prefix:' >&2

        select prefix in 'ICON' 'PIC'; do
        case $REPLY in
        1) break ;;
        2) break ;;
        *) echo 'Try again' >&2
        esac
        done

        rename 's/.*([0-9][0-9][0-9].jpg)/'"$prefix"'$1/' *[0-9][0-9][0-9].jpg

        echo 'Done' >&2


        I've chosen to use select as this produces a nice looking menu of the available options. I'm also applying the rename command only to the files that I know can be renamed (there may be other JPEG files in the directory that does not have the three digits, for example). All output from this script is strictly user interaction, so I'm doing this on the standard error stream (with >&2).




        The issues with your original script are




        1. [[ must be followed by a space.

        2. Double quotes are "...", not “...”.

        3. The read utility takes a variable name, not a value.

        4. There is a dangling done at the end of the code.


        5. elif expects a test (not an if).

        A suggestion would be to run you scripts through a syntax checker, such as the one available online at https://www.shellcheck.net/



        Also, when asking questions like these, it is quite important to include the exact error message produced by the script, and also to mention what it is that you actually want to do (so that we don't have to reverse-engineer your code). See e.g. Jeff's excellent Meta question-and-answer about asking well received questions.






        share|improve this answer














        To replace all but the last three digits and the filename suffix of all JPEG files in the current directory with either the word PIC or ICON, you may do the following (this interactively asks for the prefix to use):



        #!/bin/bash

        echo 'Please select prefix:' >&2

        select prefix in 'ICON' 'PIC'; do
        case $REPLY in
        1) break ;;
        2) break ;;
        *) echo 'Try again' >&2
        esac
        done

        rename 's/.*([0-9][0-9][0-9].jpg)/'"$prefix"'$1/' *[0-9][0-9][0-9].jpg

        echo 'Done' >&2


        I've chosen to use select as this produces a nice looking menu of the available options. I'm also applying the rename command only to the files that I know can be renamed (there may be other JPEG files in the directory that does not have the three digits, for example). All output from this script is strictly user interaction, so I'm doing this on the standard error stream (with >&2).




        The issues with your original script are




        1. [[ must be followed by a space.

        2. Double quotes are "...", not “...”.

        3. The read utility takes a variable name, not a value.

        4. There is a dangling done at the end of the code.


        5. elif expects a test (not an if).

        A suggestion would be to run you scripts through a syntax checker, such as the one available online at https://www.shellcheck.net/



        Also, when asking questions like these, it is quite important to include the exact error message produced by the script, and also to mention what it is that you actually want to do (so that we don't have to reverse-engineer your code). See e.g. Jeff's excellent Meta question-and-answer about asking well received questions.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 22 mins ago

























        answered 39 mins ago









        Kusalananda

        111k15216343




        111k15216343




















            Suh Fru Kieran Claver is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            Suh Fru Kieran Claver is a new contributor. Be nice, and check out our Code of Conduct.












            Suh Fru Kieran Claver is a new contributor. Be nice, and check out our Code of Conduct.











            Suh Fru Kieran Claver is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f478095%2fcorrect-this-script%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)