Find the answer to your Linux question:
Results 1 to 8 of 8
Hi All, I was creating a number of aliases last night in CentOS, and thought, couldn't I just create a shell script, my 1st, to do this auto. So, I ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2005
    Posts
    10

    Alias Auto Script


    Hi All,

    I was creating a number of aliases last night in CentOS, and thought, couldn't I just create a shell script, my 1st, to do this auto. So, I thought about it some, and looked at examples in my Shell Scripting in 24hrs book, but, am a little lost with it. So, without asking people to post example scxripts, as that would be a cop out, could someone just give me some scripting ideas(logic etc).? Appreciate, in advance.

  2. #2
    Linux Engineer spencerf's Avatar
    Join Date
    Jan 2005
    Location
    Syracuse, NY
    Posts
    1,144
    what do you mean by "aliases"? Are you just creating an alias like

    BURN=growisofs -dvd-compat -speed=2 /dev/sr0=/home/jeff/1.iso

    so to burn a file called 1.iso all you have to do is type BURN in the shell. If this is what you want to do you can permanely add aliases in your .bashrc file located in your home.

    so for me it is: nano /home/bob/.bashrc

    add appropriate ALIAS and it will permanently be saved. If this is not what you mean then sorry for the fluff.
    All right, brain. You don't like me and I don't like you, but let's just do this and I can get back to killing you with beer. All New Users Read This!!! If you have a grub problem please look at GRUB MANUAL

  3. #3
    Just Joined!
    Join Date
    Dec 2005
    Posts
    10
    Quote Originally Posted by spencerf
    what do you mean by "aliases"? Are you just creating an alias like

    BURN=growisofs -dvd-compat -speed=2 /dev/sr0=/home/jeff/1.iso

    so to burn a file called 1.iso all you have to do is type BURN in the shell. If this is what you want to do you can permanely add aliases in your .bashrc file located in your home.

    so for me it is: nano /home/bob/.bashrc

    add appropriate ALIAS and it will permanently be saved. If this is not what you mean then sorry for the fluff.
    yeah man, that's it..I know I can go to the ~/.bashrc file, but, man, that's an awful lottta typing, when you wanna create lotsa aliases..I wanted to auto the process...a script, that asks for alias name, alias cmd, and then appends the new alias to the .bashrc file..I'd only need to add the alias name, and alias cmd, nuthin more..anyway, it's a not a big deal, just thought it'd b a cool 1st script...

  4. #4
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    that script is pretty easy.

    Firstly you ask for the name, and then give the name a variable and then you ask for the command and give the command a variable, and then you redirect the contents to the end of the .bashrc file.

    Something like this should do it:
    Code:
    #!/bin/bash
    
    echo -n "Alias name: "
    read name
    
    echo -n "Alias command: "
    read command
    
    #echo's the command to the end of .bashrc
    echo "alias $name='$command'" >> ~/.bashrc
    
    #updates the .bashrc the shell uses
    source ~/.bashrc
    though it might need fine tuning.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  5. #5
    Just Joined!
    Join Date
    Dec 2005
    Posts
    10
    Hi All,

    cheers, and below is what I came up with a little earlier,


    #!/bin/bash
    #set aliasNew, aliasCommand, yesNo to zero

    aliasNew=""
    aliasCommand=""
    yesNo=""

    #read in aliasNew and aliasCommand via function newAlias

    newAlias () {
    echo -n "Please insert alias name: "
    read aliasNew
    echo -n "Please insert alias command: "
    read aliasCommand

    #confirm aliasNew and aliasCommand and append to .bashrc file...we could also check for duplicate aliases at this point before appending

    echo -n "alias $aliasNew='$aliasCommand' Is this correct (Y/N)"
    read yesNo
    case $yesNo in
    Yes|yes|Y|y)echo "alias $aliasNew='$aliasCommand'" >> ~/.bashrc ;;
    No|no|N|n)return 1 ;;
    *)echo "Error, please choose Yes or No" ;;
    esac

    #ascertain if user wants to create another alias.

    echo -n "Do you wish to create another new alias (Y/N)"
    read yesNo
    case $yesNo in
    Yes|yes|Y|y)newAlias ;;
    No|no|N|n)return 1 ;;
    *)echo "Error, please choose Yes or No" ;;
    esac
    }

    #newAlias function is first called
    newAlias


    Some question, though. If I don't input anything that * covers in the case, for yesNo, it gives the error, then starts the function again. I'd like to have it go back to the Y/N option, then continue, with variables intact. Hope I explained myself well enough. Also, if you think it can be improved, then cool, go ahead and show me.

  6. #6
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    You need to have a loop around the yes/no questions, something like this should do:

    Code:
    while [ $yesNo = * ]; do
    case $yesNo in
    Yes|yes|Y|y)echo "alias $aliasNew='$aliasCommand'" >> ~/.bashrc ;;
    No|no|N|n)return 1 ;;
    *)echo "Error, please choose Yes or No" ;;
    esac
    done
    I'm not 100% that the above code will work, but from it you should at least get the idea.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

  7. #7
    Just Joined!
    Join Date
    Dec 2005
    Posts
    10
    Hi All,

    ok, will try that. Question: Can you case within a case like below,


    #!/bin/bash
    #set aliasNew, aliasCommand, yesNo to zero

    aliasNew=""
    aliasCommand=""
    yesNo=""

    #read in aliasNew and aliasCommand via function newAlias

    newAlias () {
    echo -n "Please insert alias name: "
    read aliasNew
    echo -n "Please insert alias command: "
    read aliasCommand

    #confirm aliasNew and aliasCommand and append to .bashrc file...also check for duplicate aliases at this point before appending

    echo -n "alias $aliasNew='$aliasCommand' Is this correct? (Y/N)"
    read yesNo
    echo -n "Do you wish to check for duplicate aliases? (Y/N)"
    read checkDupli
    case $checkDupli in
    No|no|N|n)case $yesNo in
    Yes|yes|Y|y)echo "alias $aliasNew='$aliasCommand'" >> ~/.bashrc ;;
    No|no|N|n)return 1 ;;
    *)echo "Error, please choose Yes or No" ;;
    esac ;;
    Yes|yes|Y|y)bashrc = ~/.bashrc
    if [-f $bashrc] ; then
    awk '/alias $aliasNew='$aliasCommand'/ {echo -n "That alias exists already: Cancel? (Y/N)"}'
    read cancel
    case cancel in
    Yes|yes|Y|y)newAlias ;;
    No|no|N|n)echo "alias $aliasNew='$aliasCommand'" >> ~/.bashrc ;;
    *)echo "Error, please choose Yes or No" ;;
    esac
    fi

    #ascertain if user wants to create another alias.

    echo -n "Do you wish to create another new alias (Y/N)"
    read yesNo
    case $yesNo in
    Yes|yes|Y|y)newAlias ;;
    No|no|N|n)return 1 ;;
    *)echo "Error, please choose Yes or No" ;;
    esac
    }

    #newAlias function is first called
    newAlias


    Or, am I getting too messy with this code.? Also, please check my awk code, as I think it's wrong too.

  8. #8
    Linux Guru dylunio's Avatar
    Join Date
    Aug 2004
    Location
    Cymru
    Posts
    4,157
    You can case within a case iirc. As for awk, I've never used it so I can't hely you there sorry.
    Registered Linux User #371543!
    Get force-get May The Source Be With You
    /dev/null
    /dev/null2

Posting Permissions

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