Find the answer to your Linux question:
Results 1 to 5 of 5
I am trying to get the following loop to work in the bash shell.... i=0 x=141 b=1 if [ $ans1 -gt 7 ]; then echo "Too many too mount with ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Nov 2005
    Location
    Chicago, IL
    Posts
    46

    Incrementing characters in linux script.......


    I am trying to get the following loop to work in the bash shell....
    i=0
    x=141
    b=1
    if [ $ans1 -gt 7 ];
    then
    echo "Too many too mount with this script! Choose 7 or less."

    else
    while [ $i != $ans1 ]
    do
    n="-e \\$x"
    mkdir -m 777 /mnt/mnt$b
    mke2fs -F /dev/sd$n $ans2
    mount /dev/sd$n /mnt/mnt$b
    ((i++))
    ((b++))
    ((x++))
    done
    fi
    $ans1 and $ans2 = are read inputs given by the user. Essentially what I am trying to do is mount scsi devices without me having to write a new script or reuse old ones. It is my way of being lazy at work. The issue that I am having is incrementing characters. Integers is one thing where you use the increment a value set to (for example) 'n' and you can easily use the function: ((n++)) and that integer is incrementing one step at a time if in a loop.
    But this only works for integers. You cannot take characters of the alphabet and do the same thing. I found a way to take the octal value of each ASCII character...increment its octal value and then output it to its ASCII form to 'n'.

    There is a problem (the only problem from what I can tell)....with this and that is what I need help with.....when I echo the value of &n, I get that ASCII value of (for example) 'a' printed to the screen, so I know that 'n' equals to 'a' at this point of the loop. But when I input &n at /dev/sd&n for it to be formatted then it would read sd\141 and not sda. It has a problem computing the original arithmatic function for 'n'. Does anybody know of a way to resolve this issue (I tried with many ways of arranging the syntax to even other commands) or another way to increment characters in the bash shell. Thank you for any help in advance.
    Also if I left something out...please let me know, because I did not input the entire script....just the portion of relevance.

  2. #2
    Just Joined!
    Join Date
    Nov 2005
    Location
    Chicago, IL
    Posts
    46
    The thing is I found other ways of resolving this issue when going into other environments and coding such as Perl. In Perl this is NO PROBLEM whatsoever. It all has to do with how you create and call out to a predefined array. The only issue here is compatibility....not all my consoles at work operate at the same OS and kernel. You have multiple version of Solaris here, different version of linux there and the bourne shell and bourne again shell are probably the most common on almost all of them. This is how easy it would be in perl:
    #!/usr/bin/perl
    @alpha = ("a","b","c","d");
    $n=0;
    print "/dev/sd$alpha[$n]\n";
    $n++;
    print "/dev/sd$alpha[$n]\n";
    $n++;
    print "/dev/sd$alpha[$n]\n";
    The output would be:
    /dev/sda
    /dev/sdb
    /dev/sdc
    Obviously I would create a loop and define my array a little cleaner....but you get the picture. If only I could resolve this issue in just a basic shell.
    If I didn't mentioned before, I am mentioning it now. I know the problem doesn't lie with the way the parameter is echoed to the screen...it is how I set the value to the variable 'n'. Many thanks in advance once again.

  3. #3
    Just Joined!
    Join Date
    Nov 2005
    Posts
    4

    How about a "number to letter" function?

    You might of already thought of this but just in case not, here is a possible (rather inefficient) solution.
    After reading, I sort of put together a script called "lendhand". It looked like this:
    #! /bin/bash
    case $1 in
    1)echo a;;
    2)echo b;;
    3)echo c;;
    esac
    I tested this in my home directory with: [homedir~]$ ls -$(lendhand 1)
    It indeed listed all of my hidden files as if it was "ls -a".
    In my personal opinion this is ghetto but if time is of the essence..........

  4. #4
    Just Joined!
    Join Date
    Nov 2005
    Location
    Chicago, IL
    Posts
    46
    upidStay,
    I thank you for the response. I will be honest with you I haven't thought of this. From what I can tell (not tested yet)....this may be able to work in the manner I wish but I was also hoping for something a little more efficient because sometimes I may have to work with 512 LUNs mapped to a 2.6 kernel host and 256 LUNs to a 2.4 kernel host...if the test case calls for it; but for the most part, between 1-8 LUNs or so. The reason why I say this is when I modprobe that scsi device for all LUNs mapped to it and it picks up 512 or 374, the /dev/sd[x] will sky rocket up to a point where I do not want to have to individually type each character(s) out and have it echoed to the screen (it is my laziness talking again). If I am working with a limited amount of mapped devices then this approach you suggested would work great and there would be no need for anything complex.
    Once again I thank you for your reply...you have been helpful. Oh and don't worry.....I am in no rush to solving this problem. Don't get me wrong, I would like an answer as soon as I can get it but there is really no rush. I can still script things the longer way for the time being.

  5. #5
    Just Joined!
    Join Date
    Nov 2005
    Location
    Chicago, IL
    Posts
    46
    For those interested...I solved my problem by taking a different approach and using a lot of sed and cut commands. You can adapt this to whatever environment you guys are working with...when needed to work with random variables.
    #!/bin/sh
    #This function was developed by Petros Koutoupis
    #Last revised on 28-11-05
    echo "In KBs, how big you scsi devices?"
    read ans

    fdisk -l|sed '/Disk \dev\sd* doesn/p' > sedtemp
    cut -s -d : -f 1 sedtemp > sedtemp1
    cut -c 6-15 sedtemp1 > sedtemp

    #Receiving number of mountable scsi devices
    n="grep -c /dev/sd sedtemp"
    eval nn=`$n`

    #Starting of loop
    i=1
    b=1
    while [ $i -le $nn ]
    do
    sed -n $[$i]p sedtemp > sedtemp2
    x="sed -n p sedtemp2"
    eval xx=`$x`
    mkdir -m 777 /mnt/mnt$b
    mke2fs -F $xx $ans
    mount $xx /mnt/mnt$b
    ((i++))
    ((b++))
    done
    rm sedtemp*
    It doesn't matter how many LUNs I have mapped to my HBA(s)....it will search and mount ALL of them. Down the road this will in turn be modified even more for other variables.

Posting Permissions

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