You are not logged in.
Instead of sda you should use $1 (i.e. the first parameter), which will be the %k in the RUN+= string in the udev rule I posted above.
Ah, right..,I will figure this out thanks for the tip;) , it really isn't easy, but once I get this right, I can write some helpful scripts myself.
Little question, hope the answer won't be big;),
The disk is passed to the script by putting '%k' parameter in the RUN part of the rule! right?, OK , than how do I 'call' the output of that in the script?
I am not sure if this is necessary... but in every script i have seen, there is whitespace between the parameters in the test brackets. So it would be [ "$_kernel" = "CK" ]. Maybe it is just for readability, but this is what I have always seen.
Yeah, you're right about that, and already changed it, I even found a page saying just that;)
Brebs is right about using a variable in place of sda. You may have only one drive, but if you had more then there is the chance that it might not end up being sda. So "$1" is the first parameter, which means that if you had a command with an argument, (say $ echo "hello" ) then that $1 would represent the first argument (in the example it would be "hello"). If you had three arguments to the command, then they could be represented as $1, $2, and $3.
I really do understand why I need to use a variable to pass the disk, but I first need to first figure out how to do that, the hint was given, not the answer;)
I need to put in some code catching that part of the udev rule I guess
Oh and the echo lines are misquoted. In that case it would actually print "bfq > /sys/block/sda/queue/scheduler". So you want the echo "bfq" to be redirected (>) into that path. Having the double quotes around that whole thing would group bfq with the path, which is not what you are trying to do. It is a good idea to get in the habit of quoting things, but in this case it isn't actually necesary since you are echoing a string with no white space. If you wanted it to print "hello world" then it would then be necessay to do echo "hello world" so that the command knows that those two words are grouped together as one argument.
Yes you're right, when I looked at it twice I get what you mean!, already read the part on quoting and whitespace, but to use it correct hmzz..
But I do understand why the quotes shouldn't be around the whole command, or could be removed!
Thanks all so far.
Offline
I really do understand why I need to use a variable to pass the disk, but I first need to first figure out how to do that, the hint was given, not the answer;)
Yeah, but we've watched you get this far. I figured i'd give you one thing for free, as the use of arguments as variables is quite a handy thing to know how to do.
Offline
brebs wrote:Instead of sda you should use $1 (i.e. the first parameter), which will be the %k in the RUN+= string in the udev rule I posted above.
Ah, right..,I will figure this out thanks for the tip;) , it really isn't easy, but once I get this right, I can write some helpful scripts myself.
Little question, hope the answer won't be big;),
The disk is passed to the script by putting '%k' parameter in the RUN part of the rule! right?, OK , than how do I 'call' the output of that in the script?WonderWoofy wrote:I am not sure if this is necessary... but in every script i have seen, there is whitespace between the parameters in the test brackets. So it would be [ "$_kernel" = "CK" ]. Maybe it is just for readability, but this is what I have always seen.
Yeah, you're right about that, and already changed it, I even found a page saying just that;)
Brebs is right about using a variable in place of sda. You may have only one drive, but if you had more then there is the chance that it might not end up being sda. So "$1" is the first parameter, which means that if you had a command with an argument, (say $ echo "hello" ) then that $1 would represent the first argument (in the example it would be "hello"). If you had three arguments to the command, then they could be represented as $1, $2, and $3.
I really do understand why I need to use a variable to pass the disk, but I first need to first figure out how to do that, the hint was given, not the answer;)
I need to put in some code catching that part of the udev rule I guess
No. That's what WonderWoofy figured to give you for free. $1 just stands for the first argument fed to the script. You don't need to do anything to catch it - the shell catches it and gives it to the script. You just use $1 in your script - you don't need to worry about getting it there.
By the way, if you like books, I've found most of the stuff O'Reilly turns out pretty good. Classic Shell Scripting covers a lot of the ground. I don't think books are necessary and they have disadvantages - stuff changes and they are out dated. But I like to have some as it is often hard to know what is possible and if you don't know it is possible, why would you search for it? Plus you can read a book on the loo or in the bath or fall asleep with it without endangering your laptop/tablet/sanity etc. (But I don't recommend them for use in the shower.)
CLI Paste | How To Ask Questions
Arch Linux | x86_64 | GPT | EFI boot | refind | stub loader | systemd | LVM2 on LUKS
Lenovo x270 | Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz | Intel Wireless 8265/8275 | US keyboard w/ Euro | 512G NVMe INTEL SSDPEKKF512G7L
Offline
So, first I took a day off...)
Yeah, but we've watched you get this far. I figured i'd give you one thing for free, as the use of arguments as variables is quite a handy thing to know how to do.
, ...that far.., I looks like nothing happened, the 'lines', I created in that first part, are not tottaly gone, there are still pieces of it in it, well, must they were just sentences, now they're actually doing something!
No. That's what WonderWoofy figured to give you for free. $1 just stands for the first argument fed to the script. You don't need to do anything to catch it - the shell catches it and gives it to the script. You just use $1 in your script - you don't need to worry about getting it there.
Right, I think I get it, watch the scriptto see if I do!
By the way, if you like books, I've found most of the stuff O'Reilly turns out pretty good. Classic Shell Scripting covers a lot of the ground. I don't think books are necessary and they have disadvantages - stuff changes and they are out dated. But I like to have some as it is often hard to know what is possible and if you don't know it is possible, why would you search for it? Plus you can read a book on the loo or in the bath or fall asleep with it without endangering your laptop/tablet/sanity etc. (But I don't recommend them for use in the shower.)
![]()
Yes, I like books, I have some like, 'Running Linux' from O'Reilly, but I don't have one of their program book. So I went trough my books, Hackers guide, could be handy, on the daily bookshelf, lots of application books, like flash java html php etc.,well what do I find here, Quickly learn to program shell, for bash and kornshell, by Markus Rathmann & Crista Wieskotten from Pearson, ..now thats nice;), copyright is 2000, but this could be a nice almost complete reference guide, I remember I picked it up for 2 euro a while ago, it was to little to leave it there, and I thought might be handy in the future,and that future has arrived ![]()
edit; and then forget to paste the script, here it is
#!/bin/bash
_kernel=$(uname -r |cut -d'-' -f3)
if [ "$_kernel" = "ARCH" ]; then
echo cfq > "/sys/block/"$1"/queue/scheduler"; fi
if [ "$_kernel" = "CK" ]; then
echo bfq > "/sys/block/"$1"/queue/scheduler"; fiLast edited by qinohe (2013-06-02 14:40:43)
Offline
With just a quick glance, that appears as though it *should* work! Way to go mark!
Just a couple formatting things. Though this could probably be chalked up to personal preference, typically most scripts will have a blank line of separation between where the variables are defined, and then the actual start of the script (in this case the satrt of your if/then statements). Also, the "if [ "$_kernel"...." stuff will typically be non indented, then an indention is added for each step "into" the script. So I would have formatted it like this:
#!/bin/bash
_kernel=$(uname -r |cut -d'-' -f3)
if [ "$_kernel" = "ARCH" ]; then
echo cfq > /sys/block/"$1"/queue/scheduler
fi
if [ "$_kernel" = "CK" ]; then
echo bfq > /sys/block/"$1"/queue/scheduler
fiYou may also notice that I took out the double quotes around the /path/to/queue/scheduler. Typically quoting paths are not necessary unless there is a space or some other oddity in the path itself. But in thise case, you actually also double quoted the $1 variable. So it would have then grouped it as "/sys/block/" then $1 and finally "/queue/scheduler". I am assuming that this is not what was intended. You *could* have written it as '/sys/block/"$1"/queue/scheduler' as the single and bould quotes wouldn't interfere with each other. But again, I think the quotes around the total path is unnecessary in this case, especially if you already have the variable quoted.
Edit: You should also be aware that using 'cut' can often be a rather hackish way to parse things. In this case, it is probably okay since the formatting of Arch package versions is so consistent. But it relies very heavily on the piped input to be formatted in an exact way. So just be aware of that. I'm not saying you need to change it or find a better way, but just lodge that in the back of your noggin for future reference.
Last edited by WonderWoofy (2013-06-02 15:22:56)
Offline
With just a quick glance, that appears as though it *should* work! Way to go mark!
Just a couple formatting things. Though this could probably be chalked up to personal preference, typically most scripts will have a blank line of separation between where the variables are defined, and then the actual start of the script (in this case the satrt of your if/then statements). Also, the "if [ "$_kernel"...." stuff will typically be non indented, then an indention is added for each step "into" the script. So I would have formatted it like this:
#!/bin/bash _kernel=$(uname -r |cut -d'-' -f3) if [ "$_kernel" = "ARCH" ]; then echo cfq > /sys/block/"$1"/queue/scheduler fi if [ "$_kernel" = "CK" ]; then echo bfq > /sys/block/"$1"/queue/scheduler fi
Yes, you know, I did it because to me it seemed more logical, to do it like that, but you're right, for readability it's beter like you show it!
You may also notice that I took out the double quotes around the /path/to/queue/scheduler. Typically quoting paths are not necessary unless there is a space or some other oddity in the path itself. But in thise case, you actually also double quoted the $1 variable. So it would have then grouped it as "/sys/block/" then $1 and finally "/queue/scheduler". I am assuming that this is not what was intended. You *could* have written it as '/sys/block/"$1"/queue/scheduler' as the single and bould quotes wouldn't interfere with each other. But again, I think the quotes around the total path is unnecessary in this case, especially if you already have the variable quoted.
I should have quoted "$1" in the echo, right? I thought I'd have to, alright, I get that single quote too!, that quoting, I need to practice a lot!
Edit: You should also be aware that using 'cut' can often be a rather hackish way to parse things. In this case, it is probably okay since the formatting of Arch package versions is so consistent. But it relies very heavily on the piped input to be formatted in an exact way. So just be aware of that. I'm not saying you need to change it or find a better way, but just lodge that in the back of your noggin for future reference.
Yes, I know!, after alphaniner said it was best to use the full uname -r and after that parse it down with bash parameter expansion, I looked it up, and he is right about being careful to use it, because of possible strange things happening, because the cut has changed, same conclusion as you said package version format is always te same... , so I decided to go with your cut solution, but I didn't just do!!
Well, I'm not totally ready, almost..!
This rules I have for udev, but I have a question about that!
Right now the script is for the non-rotational disk!
So, can I just have 2 line than, 1 for non- and 1 for rotational-disk, like this!?
# script sets scheduler for rotating disk
ACTION=="add|change", KERNEL=="sd[a-z]", RUN+="/root/bin/iodrive %k"
# set cfq scheduler for rotating disk
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"Or could I add the rotating disk to that script too?
edit; well I think I leave the line for rotational as is, we have only one variable, ofcourse, I could create a second one, is this possible?
#!/bin/bash
sdb="$2"
_kernel=$(uname -r |cut -d'-' -f3)
if [ "$_kernel" = "ARCH" ]; then
echo cfq > /sys/block/"$1"/queue/scheduler
echo cfq > /sys/block/"$2"/queue/scheduler
fi
if [ "$_kernel" = "CK" ]; then
echo bfq > /sys/block/"$1"/queue/scheduler
echo cfq > /sys/block/"$2"/queue/scheduler
fiAnd do it all with that one udev rule!
Last edited by qinohe (2013-06-02 16:52:32)
Offline
Well what I was thinking when I created these rules while still using the CK kernel, was that I always like to set my scheduler to deadline when it is non-rotational (just habit I guess). So there is no need for scripting in that case, since it would be the same, and compatible across all kernels. But the rotational is where I wanted to set bfq if available, and cfq otherwise. So I think it would be like this:
# set scheduler for non-rotating disks
ACTION=="add", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="deadline"
# set scheduler for rotating disks
ACTION=="add", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="/usr/local/bin/iodrive %k"As you have it set up in the post above, all drives would be matched in the first rule, the scheduler would be set to whatever the script does, then rotational drives would be matched again in the second part of the rules file, and the scheduler would be changed again. That just seems inefficient and a waste of processes. So I think it would be better to match them explicitlly in both rather than generally then explicity.
The sdb="$2" doesn't really make sense here. As it is, with "/usr/bin/iodrive %k" you don't ave a second argument. The "%k" is the first, and there is nothing after it. So you are back to making assumptions about how the devices are going to be polled. Apart from those assumptions, that variable would be saying that sdb=$2, so $sdb would expand to $2, which would expand to nothing. If were were to suppose that the assumption were in fact the correct way, you would want it to be 2=sdb. But even then, you are treading into territory of creating interference with standard scripting behavior. So if you were dead set on making it involve a 2, it would probably be wiser to do _2=sdb. But again, that is not what you are actually after here.
Edit: BTW, I try to keep eveything I can out of /usr/bin that is not put there by pacman. This kind of stuff fits a whole lot better into /usr/local/bin. /usr/local is where your user stuff should go. So if you are compiling something w/o pacman (which is not recommended) you would do "./configure --prefix=/usr/local" so that things don't end up in /usr/bin like the normal packages do.
Last edited by WonderWoofy (2013-06-02 20:51:20)
Offline
Well what I was thinking when I created these rules while still using the CK kernel, was that I always like to set my scheduler to deadline when it is non-rotational (just habit I guess). So there is no need for scripting in that case, since it would be the same, and compatible across all kernels. But the rotational is where I wanted to set bfq if available, and cfq otherwise. So I think it would be like this:
# set scheduler for non-rotating disks ACTION=="add", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="deadline" # set scheduler for rotating disks ACTION=="add", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="/usr/local/bin/iodrive %k"As you have it set up in the post above, all drives would be matched in the first rule, the scheduler would be set to whatever the script does, then rotational drives would be matched again in the second part of the rules file, and the scheduler would be changed again. That just seems inefficient and a waste of processes. So I think it would be better to match them explicitlly in both rather than generally then explicity.
This I found out when I tested the script, it didn;t work, so I placed it in "/usr/local/bin" as you suggested, and changed the udev rule a little bit.
Btw. I'm very glad I choose this setup, even if anyone thinks it's not a good one, because this made me start to learn to script in the first place;)
The sdb="$2" doesn't really make sense here. As it is, with "/usr/bin/iodrive %k" you don't ave a second argument. The "%k" is the first, and there is nothing after it. So you are back to making assumptions about how the devices are going to be polled. Apart from those assumptions, that variable would be saying that sdb=$2, so $sdb would expand to $2, which would expand to nothing. If were were to suppose that the assumption were in fact the correct way, you would want it to be 2=sdb. But even then, you are treading into territory of creating interference with standard scripting behavior. So if you were dead set on making it involve a 2, it would probably be wiser to do _2=sdb. But again, that is not what you are actually after here.
I actually did it right the first time, and started adding things, and then it becomes messy, I found out it is really important to follow the rules, otherwise it won't work.
And yes, I understand the use of that second argument a little better now, but I still have questions about that, but not now, it's very late, and early day!
Edit: BTW, I try to keep eveything I can out of /usr/bin that is not put there by pacman. This kind of stuff fits a whole lot better into /usr/local/bin. /usr/local is where your user stuff should go. So if you are compiling something w/o pacman (which is not recommended) you would do "./configure --prefix=/usr/local" so that things don't end up in /usr/bin like the normal packages do.
See above.
The script is the same as the fore-last, and my udev rule look like this;
# set deadline scheduler for non-rotating disks
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="0", RUN+="/usr/local/bin/iodrive %k"
# set cfq scheduler for kernel=Linux. set bfq scheduler for kernel=Linux-CK
ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{queue/rotational}=="1", ATTR{queue/scheduler}="cfq"Thanks Curtis,Now I'm very happy, and a liitle proud, as the script is working now,
Thank you all for helping me out
,for your patience, and guidance on difficult (for me) issues.
Offline