how to add new options in bash script

what i want to be is that i would like to reinvent new options that have the same functionality as
...
-u is the same functionality of conv=ucase
and -l have the same functionality as conv=lcase...

is there a way that i can put these in my script so that
whenever i will typed in command line such as

dd if=file of=file.new -u

so that the file contents will become upper case ... Please need help....

For the amount of work you need to do the return is minimal. And there is no such thing as 'adding an option' without doing a lot of coding. Also, if you use /usr/bin/dd instead of just plain dd, you will get the old behavior, not what you want.

  1. create a script that turns -l into conv=lcase and -u into conv=lcase
#!/bin/bash
cmd="$*" 
cmd= cmd=$( echo "$cmd" |  sed 's/dd /\/usr\/bin\/dd /'| sed 's/\-u/conv=ucase /' | sed  's/\-l/conv=lcase /' )
eval $cmd

Be sure to add execute permissions to you new script:

chmod +x nameofnewscript 
  1. edit your login .profile or .bashrc file to create an alias for the script
alias dd=/path/to/mydd.shl
  1. log out and then back in, your new alias will be shown in the output of:
alias

As an aside I don't see why you'd want to go to all this trouble....

ok thank you very much for your great help... i will try this later after my class... thanks and god bless

Just for clarity here (there is absolutely no critical intent) - when using sed and there are a lot of escaped slashes I use a different separator - to make things easier to read.

#!/bin/bash
cmd="$*" 
cmd= cmd=$( echo "$cmd" |  sed 's/dd /\/usr\/bin\/dd /'| sed 's/\-u/conv=ucase /' | sed  's/\-l/conv=lcase /' )
eval $cmd

I would enter that as follows.

#!/bin/bash
cmd="$*" 
cmd= cmd=$( echo "$cmd" |  sed 's;dd ;/usr/bin/dd ;'| sed 's;\-u;conv=ucase ;' | sed  's;\-l;conv=lcase ;' )
eval $cmd

You can use any character that you want here, as I said just improves readability IMHO.:slight_smile:

Regards

Dave

I would use a function called dd() and call /bin/dd inside it after substituting the options

I would do none of the above. IMO: There is no discernable purpose (other than homework which this now appears to be) to doing any of this.

There are instances for using an alias (or a function) when for security reasons you want to do something like flip off/on ftp or sftp without breaking anything. Not because you want to spend cpu cycles so you can type "-u" instead of "conv=ucase"

Does anybody remember trigraphs in C? They came about because some early keyboards could not create certain characters. There had to be a workaround. Doing this "entry workaround" kind of thing is not inherently purposeless.

Since I cannot tell if this is homework, I'm leaving the thread open. Another mod may not do that.

i try this at home and theres a problem...
the name of my script is mydd
and i do the above like what it is said... but when i type in command line
bash@ubuntu$: dd if=mydd of=mydd.new -u
bash@ubuntu$: /path/to/mydd.sh file or directory not found

why is it??

Probably because your PATH variable does not include the path/to/mydd.

hmf.. i dont get it... what is that mean

---------- Post updated at 07:44 PM ---------- Previous update was at 07:38 PM ----------

where can i see btw path variable

When i typed by in command line
pth

output is :

/usr/bin/...

by the way, what will i put in my path variable??

As an example;

pwd
/usr/local/bin
export PATH=$PATH:/usr/local/bin
env | grep PATH
PATH=/usr/bin:/usr/sbin:/usr/local/bin

Regards

Dave

PATH is upper case. It works like this

  1. you place a directory in it
  2. you place a separator which is a colon

The order you place the directory, is the order the system searches for a command.

# show your PATH variable
echo $PATH
# show every environment variable you have.
set
# show everything set shows and some more
env

theres a problem in here...
in my commandline
when i typed

ubuntu@ubuntu$: pwd
it says
home/ubuntu

and in my .bashrc
i put these one

alias dd=/path/to/mydd.shl
and save it

and in my new script name mydd:

pico mydd

iput these code:

#!/bin/bash
cmd="$*"
cmd= cmd=$( echo "$cmd" | sed 's/dd /\/usr\/bin\/dd /'| sed 's/\-u/conv=ucase /' | sed 's/\-l/conv=lcase /' )
eval $cmd
and chmod +x mydd

and when i typed in command line:

ubuntu@ubuntu$: dd if=mydd of=mydd.new -u
ubntu@ubuntu$ : /path/to/mydd.shl is not file or directory

i would like the output for mydd will become upper case all
but theres a problem...