How can I add a third parameter to this bash script?












-1















I want to add the third parameter that will be changing files name from upper to lower OR lower to upper but in this third parameter I want to specify what file's name must be changed? What's wrong with this script? Thank you in advance.



#!/bin/bash

if test "$1" = "lower" && test "$2" = "upper"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
fi
done

elif test "$1" = "upper" && test "$2" = "lower"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:upper:] [:lower:])";
fi
done
fi

if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ];
then
for file in * ; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi









share|improve this question

























  • The indentation, for a start. Could you fix it so it's legible?

    – tripleee
    Nov 23 '18 at 9:26











  • What do you hope for the comparison against $0 to actually accomplish?

    – tripleee
    Nov 23 '18 at 9:27











  • You should use double quotes around variables which contain file names, everywhere; and the arguments to tr should probably be in single quotes.

    – tripleee
    Nov 23 '18 at 9:28











  • What's wrong in this script? At least this line: if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ]; then. The $file variable will contain the last file name, that was processed in first if block, and it's not guaranteed in any way that it will be exactly the same file as in $3. (Even if it is in the current directory, it could've been processed at the beginning or in the middle of the for loop in first if block).

    – Yoory N.
    Nov 23 '18 at 9:36













  • Start by writing a "usage screen" that explains the expected inputs and output. That usually helps me figure out how I want to write the code.

    – Paul Hodges
    Nov 23 '18 at 14:32
















-1















I want to add the third parameter that will be changing files name from upper to lower OR lower to upper but in this third parameter I want to specify what file's name must be changed? What's wrong with this script? Thank you in advance.



#!/bin/bash

if test "$1" = "lower" && test "$2" = "upper"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
fi
done

elif test "$1" = "upper" && test "$2" = "lower"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:upper:] [:lower:])";
fi
done
fi

if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ];
then
for file in * ; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi









share|improve this question

























  • The indentation, for a start. Could you fix it so it's legible?

    – tripleee
    Nov 23 '18 at 9:26











  • What do you hope for the comparison against $0 to actually accomplish?

    – tripleee
    Nov 23 '18 at 9:27











  • You should use double quotes around variables which contain file names, everywhere; and the arguments to tr should probably be in single quotes.

    – tripleee
    Nov 23 '18 at 9:28











  • What's wrong in this script? At least this line: if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ]; then. The $file variable will contain the last file name, that was processed in first if block, and it's not guaranteed in any way that it will be exactly the same file as in $3. (Even if it is in the current directory, it could've been processed at the beginning or in the middle of the for loop in first if block).

    – Yoory N.
    Nov 23 '18 at 9:36













  • Start by writing a "usage screen" that explains the expected inputs and output. That usually helps me figure out how I want to write the code.

    – Paul Hodges
    Nov 23 '18 at 14:32














-1












-1








-1








I want to add the third parameter that will be changing files name from upper to lower OR lower to upper but in this third parameter I want to specify what file's name must be changed? What's wrong with this script? Thank you in advance.



#!/bin/bash

if test "$1" = "lower" && test "$2" = "upper"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
fi
done

elif test "$1" = "upper" && test "$2" = "lower"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:upper:] [:lower:])";
fi
done
fi

if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ];
then
for file in * ; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi









share|improve this question
















I want to add the third parameter that will be changing files name from upper to lower OR lower to upper but in this third parameter I want to specify what file's name must be changed? What's wrong with this script? Thank you in advance.



#!/bin/bash

if test "$1" = "lower" && test "$2" = "upper"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
fi
done

elif test "$1" = "upper" && test "$2" = "lower"
then
for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:upper:] [:lower:])";
fi
done
fi

if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ];
then
for file in * ; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi






linux bash terminal






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 17:51









jww

53.2k40228500




53.2k40228500










asked Nov 23 '18 at 9:22









versacesversaces

186




186













  • The indentation, for a start. Could you fix it so it's legible?

    – tripleee
    Nov 23 '18 at 9:26











  • What do you hope for the comparison against $0 to actually accomplish?

    – tripleee
    Nov 23 '18 at 9:27











  • You should use double quotes around variables which contain file names, everywhere; and the arguments to tr should probably be in single quotes.

    – tripleee
    Nov 23 '18 at 9:28











  • What's wrong in this script? At least this line: if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ]; then. The $file variable will contain the last file name, that was processed in first if block, and it's not guaranteed in any way that it will be exactly the same file as in $3. (Even if it is in the current directory, it could've been processed at the beginning or in the middle of the for loop in first if block).

    – Yoory N.
    Nov 23 '18 at 9:36













  • Start by writing a "usage screen" that explains the expected inputs and output. That usually helps me figure out how I want to write the code.

    – Paul Hodges
    Nov 23 '18 at 14:32



















  • The indentation, for a start. Could you fix it so it's legible?

    – tripleee
    Nov 23 '18 at 9:26











  • What do you hope for the comparison against $0 to actually accomplish?

    – tripleee
    Nov 23 '18 at 9:27











  • You should use double quotes around variables which contain file names, everywhere; and the arguments to tr should probably be in single quotes.

    – tripleee
    Nov 23 '18 at 9:28











  • What's wrong in this script? At least this line: if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ]; then. The $file variable will contain the last file name, that was processed in first if block, and it's not guaranteed in any way that it will be exactly the same file as in $3. (Even if it is in the current directory, it could've been processed at the beginning or in the middle of the for loop in first if block).

    – Yoory N.
    Nov 23 '18 at 9:36













  • Start by writing a "usage screen" that explains the expected inputs and output. That usually helps me figure out how I want to write the code.

    – Paul Hodges
    Nov 23 '18 at 14:32

















The indentation, for a start. Could you fix it so it's legible?

– tripleee
Nov 23 '18 at 9:26





The indentation, for a start. Could you fix it so it's legible?

– tripleee
Nov 23 '18 at 9:26













What do you hope for the comparison against $0 to actually accomplish?

– tripleee
Nov 23 '18 at 9:27





What do you hope for the comparison against $0 to actually accomplish?

– tripleee
Nov 23 '18 at 9:27













You should use double quotes around variables which contain file names, everywhere; and the arguments to tr should probably be in single quotes.

– tripleee
Nov 23 '18 at 9:28





You should use double quotes around variables which contain file names, everywhere; and the arguments to tr should probably be in single quotes.

– tripleee
Nov 23 '18 at 9:28













What's wrong in this script? At least this line: if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ]; then. The $file variable will contain the last file name, that was processed in first if block, and it's not guaranteed in any way that it will be exactly the same file as in $3. (Even if it is in the current directory, it could've been processed at the beginning or in the middle of the for loop in first if block).

– Yoory N.
Nov 23 '18 at 9:36







What's wrong in this script? At least this line: if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ "$3" = "$file" ]; then. The $file variable will contain the last file name, that was processed in first if block, and it's not guaranteed in any way that it will be exactly the same file as in $3. (Even if it is in the current directory, it could've been processed at the beginning or in the middle of the for loop in first if block).

– Yoory N.
Nov 23 '18 at 9:36















Start by writing a "usage screen" that explains the expected inputs and output. That usually helps me figure out how I want to write the code.

– Paul Hodges
Nov 23 '18 at 14:32





Start by writing a "usage screen" that explains the expected inputs and output. That usually helps me figure out how I want to write the code.

– Paul Hodges
Nov 23 '18 at 14:32












3 Answers
3






active

oldest

votes


















0














If I am guessing correctly what you want, try



#!/bin/bash

case $1:$2 in
upper:lower | lower:upper ) ;;
*) echo "Syntax: $0 upper|lower lower|upper files ..." >&2; exit 1;;
esac

from=$1
to=$2
shift; shift
for file; do
mv "$file" "$(echo "$file" | tr "[:$from:]" "[:$to:]")"
done


This has the distinct advantage that it allows more than three arguments, where the first two specify the operation to perform.



Notice also how we take care to always quote strings which contain a file name. See also When to wrap quotes around a shell variable?



The above script should in fact also work with /bin/sh; we do not use any Bash-only features so it should run under any POSIX sh.



However, a much better design would probably be to use an option to decide what mapping to apply, and simply accept a (possibly empty) list of options and a list of file name arguments. Then you can use Bash built-in parameter expansion, too. Case conversion parameter expansion operations are available in Bash 4 only, though.



#!/bin/bash

op=',,'
# XXX FIXME: do proper option parsing
case $1 in -u) op='^^'; shift;; esac

for file; do
eval mv "$file" "${file$op}"
done


This converts to lowercase by default, and switches to uppercase instead if you pass in -u before the file names.



In both of these scripts, we use for file as a shorthand for for file in "$@" i.e. we loop over the (remaining) command-line arguments. Perhaps this is the detail you were looking for.






share|improve this answer


























  • I don't think the comparisons against "$0" were doing anything useful. If you can explain what they were supposed to accomplish, perhaps we can revisit.

    – tripleee
    Nov 23 '18 at 11:28



















0














Forgive me if I grossly misunderstand, but I think you may have misunderstood how argument passing works.



The named/numbered arguments represent the values you pass in on the command line in their ordinal positions. Each can theoretically have any value that can by stuck in a string. You don't need a third parameter, just a third value.



Let's try a sample.



#! /bin/env bash

me=${0#*/} # strip the path
use="
$me { upper | lower } file

changes the NAME of the file given to be all upper or all lower case.

"
# check for correct arguments
case $# in
2) : exactly 2 arguments passed - this is correct ;;
*) echo "Incorrect usage - $me requires exactly 2 arguments $use" >&2
exit 1 ;;
esac

declare -l lower action # these variables will downcase anything put in them
declare -u upper # this one will upcase anything in it
declare newname # create a target variable with unspecified case

action="$1" # stored the *lowercased* 1st argument passed as $action
case $action in # passed argument has been lowercased for simpler checking
upper) upper="$2" # store *uppercased* 2nd arg.
newname="$upper" # newname is now uppercase.
;;
lower) lower="$2" # store *lowercased* 2nd arg.
newname="$lower" # newname is now lowercase.
;;
*) echo "Incorrect usage - $me requires 2nd arg to be 'upper' or 'lower' $use" >&2
exit 1 ;;
esac

if [[ -e "$2" ]] # confirm the argument exists
then echo "Renaming $2 -> $newname:"
ls -l "$2"
echo " -> "
mv "$2" "$newname" # rename the file
ls -l "$newname"
else echo "'$2' does not exist. $use" >&2
exit 1
fi





share|improve this answer
























  • $0 is the name (including any pathing info used) of the program as executed. Note that at the top I stripped path info to name the program in a variable for messages throughout.

    – Paul Hodges
    Nov 23 '18 at 15:02



















-1














First of all there is indentation problem with this script check first if condition done should be coming before fi



Below is the correct.



if test "$1" = "lower" && test "$2" = "upper"
then

for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi


Secondly the question you asked:



#/bin/bash -xe 

[ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1
if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f "$3" ];
then
mv "$3" "$(echo $3 | tr [:lower:] [:upper:])";
fi


Hope this helps.






share|improve this answer


























  • I mean that when I write in command line: ./changeFileCase lower upper name_of_file, then only this name of file will be changed from lower to upper. I don't know how to write this in the script.

    – versaces
    Nov 23 '18 at 10:23






  • 1





    Check this script -> #/bin/bash -xe [ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1 if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f $3 ]; then mv "$3" "$(echo $3 | tr [:lower:] [:upper:])"; fi let me know in case any thing more you needed

    – saurav omar
    Nov 23 '18 at 10:31













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
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',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443772%2fhow-can-i-add-a-third-parameter-to-this-bash-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If I am guessing correctly what you want, try



#!/bin/bash

case $1:$2 in
upper:lower | lower:upper ) ;;
*) echo "Syntax: $0 upper|lower lower|upper files ..." >&2; exit 1;;
esac

from=$1
to=$2
shift; shift
for file; do
mv "$file" "$(echo "$file" | tr "[:$from:]" "[:$to:]")"
done


This has the distinct advantage that it allows more than three arguments, where the first two specify the operation to perform.



Notice also how we take care to always quote strings which contain a file name. See also When to wrap quotes around a shell variable?



The above script should in fact also work with /bin/sh; we do not use any Bash-only features so it should run under any POSIX sh.



However, a much better design would probably be to use an option to decide what mapping to apply, and simply accept a (possibly empty) list of options and a list of file name arguments. Then you can use Bash built-in parameter expansion, too. Case conversion parameter expansion operations are available in Bash 4 only, though.



#!/bin/bash

op=',,'
# XXX FIXME: do proper option parsing
case $1 in -u) op='^^'; shift;; esac

for file; do
eval mv "$file" "${file$op}"
done


This converts to lowercase by default, and switches to uppercase instead if you pass in -u before the file names.



In both of these scripts, we use for file as a shorthand for for file in "$@" i.e. we loop over the (remaining) command-line arguments. Perhaps this is the detail you were looking for.






share|improve this answer


























  • I don't think the comparisons against "$0" were doing anything useful. If you can explain what they were supposed to accomplish, perhaps we can revisit.

    – tripleee
    Nov 23 '18 at 11:28
















0














If I am guessing correctly what you want, try



#!/bin/bash

case $1:$2 in
upper:lower | lower:upper ) ;;
*) echo "Syntax: $0 upper|lower lower|upper files ..." >&2; exit 1;;
esac

from=$1
to=$2
shift; shift
for file; do
mv "$file" "$(echo "$file" | tr "[:$from:]" "[:$to:]")"
done


This has the distinct advantage that it allows more than three arguments, where the first two specify the operation to perform.



Notice also how we take care to always quote strings which contain a file name. See also When to wrap quotes around a shell variable?



The above script should in fact also work with /bin/sh; we do not use any Bash-only features so it should run under any POSIX sh.



However, a much better design would probably be to use an option to decide what mapping to apply, and simply accept a (possibly empty) list of options and a list of file name arguments. Then you can use Bash built-in parameter expansion, too. Case conversion parameter expansion operations are available in Bash 4 only, though.



#!/bin/bash

op=',,'
# XXX FIXME: do proper option parsing
case $1 in -u) op='^^'; shift;; esac

for file; do
eval mv "$file" "${file$op}"
done


This converts to lowercase by default, and switches to uppercase instead if you pass in -u before the file names.



In both of these scripts, we use for file as a shorthand for for file in "$@" i.e. we loop over the (remaining) command-line arguments. Perhaps this is the detail you were looking for.






share|improve this answer


























  • I don't think the comparisons against "$0" were doing anything useful. If you can explain what they were supposed to accomplish, perhaps we can revisit.

    – tripleee
    Nov 23 '18 at 11:28














0












0








0







If I am guessing correctly what you want, try



#!/bin/bash

case $1:$2 in
upper:lower | lower:upper ) ;;
*) echo "Syntax: $0 upper|lower lower|upper files ..." >&2; exit 1;;
esac

from=$1
to=$2
shift; shift
for file; do
mv "$file" "$(echo "$file" | tr "[:$from:]" "[:$to:]")"
done


This has the distinct advantage that it allows more than three arguments, where the first two specify the operation to perform.



Notice also how we take care to always quote strings which contain a file name. See also When to wrap quotes around a shell variable?



The above script should in fact also work with /bin/sh; we do not use any Bash-only features so it should run under any POSIX sh.



However, a much better design would probably be to use an option to decide what mapping to apply, and simply accept a (possibly empty) list of options and a list of file name arguments. Then you can use Bash built-in parameter expansion, too. Case conversion parameter expansion operations are available in Bash 4 only, though.



#!/bin/bash

op=',,'
# XXX FIXME: do proper option parsing
case $1 in -u) op='^^'; shift;; esac

for file; do
eval mv "$file" "${file$op}"
done


This converts to lowercase by default, and switches to uppercase instead if you pass in -u before the file names.



In both of these scripts, we use for file as a shorthand for for file in "$@" i.e. we loop over the (remaining) command-line arguments. Perhaps this is the detail you were looking for.






share|improve this answer















If I am guessing correctly what you want, try



#!/bin/bash

case $1:$2 in
upper:lower | lower:upper ) ;;
*) echo "Syntax: $0 upper|lower lower|upper files ..." >&2; exit 1;;
esac

from=$1
to=$2
shift; shift
for file; do
mv "$file" "$(echo "$file" | tr "[:$from:]" "[:$to:]")"
done


This has the distinct advantage that it allows more than three arguments, where the first two specify the operation to perform.



Notice also how we take care to always quote strings which contain a file name. See also When to wrap quotes around a shell variable?



The above script should in fact also work with /bin/sh; we do not use any Bash-only features so it should run under any POSIX sh.



However, a much better design would probably be to use an option to decide what mapping to apply, and simply accept a (possibly empty) list of options and a list of file name arguments. Then you can use Bash built-in parameter expansion, too. Case conversion parameter expansion operations are available in Bash 4 only, though.



#!/bin/bash

op=',,'
# XXX FIXME: do proper option parsing
case $1 in -u) op='^^'; shift;; esac

for file; do
eval mv "$file" "${file$op}"
done


This converts to lowercase by default, and switches to uppercase instead if you pass in -u before the file names.



In both of these scripts, we use for file as a shorthand for for file in "$@" i.e. we loop over the (remaining) command-line arguments. Perhaps this is the detail you were looking for.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 11:19

























answered Nov 23 '18 at 11:25









tripleeetripleee

91.4k13129184




91.4k13129184













  • I don't think the comparisons against "$0" were doing anything useful. If you can explain what they were supposed to accomplish, perhaps we can revisit.

    – tripleee
    Nov 23 '18 at 11:28



















  • I don't think the comparisons against "$0" were doing anything useful. If you can explain what they were supposed to accomplish, perhaps we can revisit.

    – tripleee
    Nov 23 '18 at 11:28

















I don't think the comparisons against "$0" were doing anything useful. If you can explain what they were supposed to accomplish, perhaps we can revisit.

– tripleee
Nov 23 '18 at 11:28





I don't think the comparisons against "$0" were doing anything useful. If you can explain what they were supposed to accomplish, perhaps we can revisit.

– tripleee
Nov 23 '18 at 11:28













0














Forgive me if I grossly misunderstand, but I think you may have misunderstood how argument passing works.



The named/numbered arguments represent the values you pass in on the command line in their ordinal positions. Each can theoretically have any value that can by stuck in a string. You don't need a third parameter, just a third value.



Let's try a sample.



#! /bin/env bash

me=${0#*/} # strip the path
use="
$me { upper | lower } file

changes the NAME of the file given to be all upper or all lower case.

"
# check for correct arguments
case $# in
2) : exactly 2 arguments passed - this is correct ;;
*) echo "Incorrect usage - $me requires exactly 2 arguments $use" >&2
exit 1 ;;
esac

declare -l lower action # these variables will downcase anything put in them
declare -u upper # this one will upcase anything in it
declare newname # create a target variable with unspecified case

action="$1" # stored the *lowercased* 1st argument passed as $action
case $action in # passed argument has been lowercased for simpler checking
upper) upper="$2" # store *uppercased* 2nd arg.
newname="$upper" # newname is now uppercase.
;;
lower) lower="$2" # store *lowercased* 2nd arg.
newname="$lower" # newname is now lowercase.
;;
*) echo "Incorrect usage - $me requires 2nd arg to be 'upper' or 'lower' $use" >&2
exit 1 ;;
esac

if [[ -e "$2" ]] # confirm the argument exists
then echo "Renaming $2 -> $newname:"
ls -l "$2"
echo " -> "
mv "$2" "$newname" # rename the file
ls -l "$newname"
else echo "'$2' does not exist. $use" >&2
exit 1
fi





share|improve this answer
























  • $0 is the name (including any pathing info used) of the program as executed. Note that at the top I stripped path info to name the program in a variable for messages throughout.

    – Paul Hodges
    Nov 23 '18 at 15:02
















0














Forgive me if I grossly misunderstand, but I think you may have misunderstood how argument passing works.



The named/numbered arguments represent the values you pass in on the command line in their ordinal positions. Each can theoretically have any value that can by stuck in a string. You don't need a third parameter, just a third value.



Let's try a sample.



#! /bin/env bash

me=${0#*/} # strip the path
use="
$me { upper | lower } file

changes the NAME of the file given to be all upper or all lower case.

"
# check for correct arguments
case $# in
2) : exactly 2 arguments passed - this is correct ;;
*) echo "Incorrect usage - $me requires exactly 2 arguments $use" >&2
exit 1 ;;
esac

declare -l lower action # these variables will downcase anything put in them
declare -u upper # this one will upcase anything in it
declare newname # create a target variable with unspecified case

action="$1" # stored the *lowercased* 1st argument passed as $action
case $action in # passed argument has been lowercased for simpler checking
upper) upper="$2" # store *uppercased* 2nd arg.
newname="$upper" # newname is now uppercase.
;;
lower) lower="$2" # store *lowercased* 2nd arg.
newname="$lower" # newname is now lowercase.
;;
*) echo "Incorrect usage - $me requires 2nd arg to be 'upper' or 'lower' $use" >&2
exit 1 ;;
esac

if [[ -e "$2" ]] # confirm the argument exists
then echo "Renaming $2 -> $newname:"
ls -l "$2"
echo " -> "
mv "$2" "$newname" # rename the file
ls -l "$newname"
else echo "'$2' does not exist. $use" >&2
exit 1
fi





share|improve this answer
























  • $0 is the name (including any pathing info used) of the program as executed. Note that at the top I stripped path info to name the program in a variable for messages throughout.

    – Paul Hodges
    Nov 23 '18 at 15:02














0












0








0







Forgive me if I grossly misunderstand, but I think you may have misunderstood how argument passing works.



The named/numbered arguments represent the values you pass in on the command line in their ordinal positions. Each can theoretically have any value that can by stuck in a string. You don't need a third parameter, just a third value.



Let's try a sample.



#! /bin/env bash

me=${0#*/} # strip the path
use="
$me { upper | lower } file

changes the NAME of the file given to be all upper or all lower case.

"
# check for correct arguments
case $# in
2) : exactly 2 arguments passed - this is correct ;;
*) echo "Incorrect usage - $me requires exactly 2 arguments $use" >&2
exit 1 ;;
esac

declare -l lower action # these variables will downcase anything put in them
declare -u upper # this one will upcase anything in it
declare newname # create a target variable with unspecified case

action="$1" # stored the *lowercased* 1st argument passed as $action
case $action in # passed argument has been lowercased for simpler checking
upper) upper="$2" # store *uppercased* 2nd arg.
newname="$upper" # newname is now uppercase.
;;
lower) lower="$2" # store *lowercased* 2nd arg.
newname="$lower" # newname is now lowercase.
;;
*) echo "Incorrect usage - $me requires 2nd arg to be 'upper' or 'lower' $use" >&2
exit 1 ;;
esac

if [[ -e "$2" ]] # confirm the argument exists
then echo "Renaming $2 -> $newname:"
ls -l "$2"
echo " -> "
mv "$2" "$newname" # rename the file
ls -l "$newname"
else echo "'$2' does not exist. $use" >&2
exit 1
fi





share|improve this answer













Forgive me if I grossly misunderstand, but I think you may have misunderstood how argument passing works.



The named/numbered arguments represent the values you pass in on the command line in their ordinal positions. Each can theoretically have any value that can by stuck in a string. You don't need a third parameter, just a third value.



Let's try a sample.



#! /bin/env bash

me=${0#*/} # strip the path
use="
$me { upper | lower } file

changes the NAME of the file given to be all upper or all lower case.

"
# check for correct arguments
case $# in
2) : exactly 2 arguments passed - this is correct ;;
*) echo "Incorrect usage - $me requires exactly 2 arguments $use" >&2
exit 1 ;;
esac

declare -l lower action # these variables will downcase anything put in them
declare -u upper # this one will upcase anything in it
declare newname # create a target variable with unspecified case

action="$1" # stored the *lowercased* 1st argument passed as $action
case $action in # passed argument has been lowercased for simpler checking
upper) upper="$2" # store *uppercased* 2nd arg.
newname="$upper" # newname is now uppercase.
;;
lower) lower="$2" # store *lowercased* 2nd arg.
newname="$lower" # newname is now lowercase.
;;
*) echo "Incorrect usage - $me requires 2nd arg to be 'upper' or 'lower' $use" >&2
exit 1 ;;
esac

if [[ -e "$2" ]] # confirm the argument exists
then echo "Renaming $2 -> $newname:"
ls -l "$2"
echo " -> "
mv "$2" "$newname" # rename the file
ls -l "$newname"
else echo "'$2' does not exist. $use" >&2
exit 1
fi






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 '18 at 14:58









Paul HodgesPaul Hodges

3,2161422




3,2161422













  • $0 is the name (including any pathing info used) of the program as executed. Note that at the top I stripped path info to name the program in a variable for messages throughout.

    – Paul Hodges
    Nov 23 '18 at 15:02



















  • $0 is the name (including any pathing info used) of the program as executed. Note that at the top I stripped path info to name the program in a variable for messages throughout.

    – Paul Hodges
    Nov 23 '18 at 15:02

















$0 is the name (including any pathing info used) of the program as executed. Note that at the top I stripped path info to name the program in a variable for messages throughout.

– Paul Hodges
Nov 23 '18 at 15:02





$0 is the name (including any pathing info used) of the program as executed. Note that at the top I stripped path info to name the program in a variable for messages throughout.

– Paul Hodges
Nov 23 '18 at 15:02











-1














First of all there is indentation problem with this script check first if condition done should be coming before fi



Below is the correct.



if test "$1" = "lower" && test "$2" = "upper"
then

for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi


Secondly the question you asked:



#/bin/bash -xe 

[ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1
if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f "$3" ];
then
mv "$3" "$(echo $3 | tr [:lower:] [:upper:])";
fi


Hope this helps.






share|improve this answer


























  • I mean that when I write in command line: ./changeFileCase lower upper name_of_file, then only this name of file will be changed from lower to upper. I don't know how to write this in the script.

    – versaces
    Nov 23 '18 at 10:23






  • 1





    Check this script -> #/bin/bash -xe [ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1 if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f $3 ]; then mv "$3" "$(echo $3 | tr [:lower:] [:upper:])"; fi let me know in case any thing more you needed

    – saurav omar
    Nov 23 '18 at 10:31


















-1














First of all there is indentation problem with this script check first if condition done should be coming before fi



Below is the correct.



if test "$1" = "lower" && test "$2" = "upper"
then

for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi


Secondly the question you asked:



#/bin/bash -xe 

[ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1
if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f "$3" ];
then
mv "$3" "$(echo $3 | tr [:lower:] [:upper:])";
fi


Hope this helps.






share|improve this answer


























  • I mean that when I write in command line: ./changeFileCase lower upper name_of_file, then only this name of file will be changed from lower to upper. I don't know how to write this in the script.

    – versaces
    Nov 23 '18 at 10:23






  • 1





    Check this script -> #/bin/bash -xe [ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1 if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f $3 ]; then mv "$3" "$(echo $3 | tr [:lower:] [:upper:])"; fi let me know in case any thing more you needed

    – saurav omar
    Nov 23 '18 at 10:31
















-1












-1








-1







First of all there is indentation problem with this script check first if condition done should be coming before fi



Below is the correct.



if test "$1" = "lower" && test "$2" = "upper"
then

for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi


Secondly the question you asked:



#/bin/bash -xe 

[ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1
if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f "$3" ];
then
mv "$3" "$(echo $3 | tr [:lower:] [:upper:])";
fi


Hope this helps.






share|improve this answer















First of all there is indentation problem with this script check first if condition done should be coming before fi



Below is the correct.



if test "$1" = "lower" && test "$2" = "upper"
then

for file in *; do
if [ $0 != "$file" ] && [ $0 != "./$file" ]; then
mv "$file" "$(echo $file | tr [:lower:] [:upper:])";
fi
done
fi


Secondly the question you asked:



#/bin/bash -xe 

[ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1
if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f "$3" ];
then
mv "$3" "$(echo $3 | tr [:lower:] [:upper:])";
fi


Hope this helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 11:19

























answered Nov 23 '18 at 9:51









saurav omarsaurav omar

767




767













  • I mean that when I write in command line: ./changeFileCase lower upper name_of_file, then only this name of file will be changed from lower to upper. I don't know how to write this in the script.

    – versaces
    Nov 23 '18 at 10:23






  • 1





    Check this script -> #/bin/bash -xe [ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1 if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f $3 ]; then mv "$3" "$(echo $3 | tr [:lower:] [:upper:])"; fi let me know in case any thing more you needed

    – saurav omar
    Nov 23 '18 at 10:31





















  • I mean that when I write in command line: ./changeFileCase lower upper name_of_file, then only this name of file will be changed from lower to upper. I don't know how to write this in the script.

    – versaces
    Nov 23 '18 at 10:23






  • 1





    Check this script -> #/bin/bash -xe [ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1 if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f $3 ]; then mv "$3" "$(echo $3 | tr [:lower:] [:upper:])"; fi let me know in case any thing more you needed

    – saurav omar
    Nov 23 '18 at 10:31



















I mean that when I write in command line: ./changeFileCase lower upper name_of_file, then only this name of file will be changed from lower to upper. I don't know how to write this in the script.

– versaces
Nov 23 '18 at 10:23





I mean that when I write in command line: ./changeFileCase lower upper name_of_file, then only this name of file will be changed from lower to upper. I don't know how to write this in the script.

– versaces
Nov 23 '18 at 10:23




1




1





Check this script -> #/bin/bash -xe [ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1 if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f $3 ]; then mv "$3" "$(echo $3 | tr [:lower:] [:upper:])"; fi let me know in case any thing more you needed

– saurav omar
Nov 23 '18 at 10:31







Check this script -> #/bin/bash -xe [ $# -ne 3 ] && echo "Usage: {lower} {upper} {fileName} " && exit 1 if [ "$1" = "lower" ] && [ "$2" = "upper" ] && [ -f $3 ]; then mv "$3" "$(echo $3 | tr [:lower:] [:upper:])"; fi let me know in case any thing more you needed

– saurav omar
Nov 23 '18 at 10:31




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53443772%2fhow-can-i-add-a-third-parameter-to-this-bash-script%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin