[SOLVED] Using dd command with user input variables
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
My situation: I have multiple drives in a system. One is the main boot drive/dev/sda and others are drives /dev/sd(X) I would like to run the below command's in a script and ask user for input. I do not want to give the user the option to run the below command on /dev/sda.
Here is the command.
Code:
mount | column -t --> list all drives in system
shred -vz -n 3 /dev/hd<X>
dd if=/dev/sd<X> bs=64K count=1 | od –c
Flow for script:
List all drives in system
Have them input b, c, d, e, etc to replace the <X> variable.
run the shred command
run the dd command
Confirm command has been executed.
Continue with the rest of the of the script.
Please let me know if you need more information
Last edited by powerplyer; 10-28-2013 at 06:47 AM.
Reason: forgot to add the shred command
Here is the code but it will not pick up all the "options"
Quote:
#!/bin/bash
# rsync using variables
mount | column -t
read -p "Which device would you like to format from the list above device /dev/sda is not an option " b c d e
echo "format starting"
shred -vz -n 3 /dev/hd$b b c d e
dd if=/dev/sd$b c d e bs=64K count=1 | od –c
echo "Format and Shred complete"
sleep 5
#!/bin/bash
mount | column -t | grep -v /dev/sda
read -p "Which device would you like to format from the list above device /dev/sda is not an option " disk
echo "format starting"
shred -vz -n 3 /dev/sd"$disk"
dd if=/dev/sd$disk bs=64K count=1 | od –c
echo "Format and Shred complete"
sleep 5
Thank you for the feedback and sorry for being unclear. I just realized that the drive I am trying to format can be sda,sdb, sdc, sde so I think I will have to leave it upto the user to pick the correct option. I also selected lshw vs mount |column -t because I do not want the user to mount and unmount things themselves. That being said here is what I would like to do with the script:
The exact command if I were to type it into bash manually for this particular example, however the sd<x> can change and there could be more then one:
Code:
shred -vz -n 3 /dev/sda
Then why do you have the following command in your code?
Code:
shred -vz -n 3 /dev/$sda sdb sdc sdd sde
When you run a command in a script, "blah" is just straight text, while "$blah" substitutes in the value of the variable called "blah".
So the following command:
Code:
shred -vz -n 3 /dev/$sda sdb sdc sdd sde
Will never expand to:
Code:
shred -vz -n 3 /dev/sda
What it WILL expand to, is:
Code:
shred -vz -n 3 /dev/{} sdb sdc sdd sde
With the contents of the variable "sda" replacing the "{}". If "sda" is an empty variable, then it will expand to an empty string, and the command that's run will be:
Code:
shred -vz -n 3 /dev/ sdb sdc sdd sde
If the "sda" variable contains something else, like, say the string "sdc", the command that's run will be:
#!/bin/bash
Options=("Quit")
Options+=( $(mount | awk '/\/dev\/sd[b-z]/{print $1}') )
ShredSelect () {
PS3='Which device would you like to format?: '
select Disk in "${Options[@]}";do
case $Disk in
Quit)
break
;;
'')
echo "Please select option 1 to ${#Options[@]}: "
;;
*)
echo $Disk # replace with your commands
PS3="Format another device?: "
select YN in Yes No;do
case $YN in
Yes)
ShredSelect
return
;;
No)
exit
;;
esac
done
;;
esac
done
return
}
ShredSelect
edit: the YesNo needs fixing, , add a *) to the case,
as always great fourm thanks for the help. I am a bit confused with all the suggestion:
I will try to make it simpler, it was my fault I think I was trying to over complicate things:
Flow:
Display all the scsi (purely for information of disk in system)
Ask the user to "Which drive he would like to run fdisk -l /dev/sd<X>"
User would input: sda
Display the fdisk -l /dev/sda
I think the code would go something like this:
Code:
#!/bin/bash
clear
lsscsi
SOURCEDIR=/home/user/Desktop
read -p "Which device would you like to read" sda sdb sdc sdd sda
fdisk -l /dev/$sda
Output
Code:
[0:1:0:0] disk LSI NWD-RLP4-1860 0002 /dev/sda
[1:0:0:0] disk ATA Corsair Force 3 5.02 /dev/sdb
Which device would you like to readsda
last_lba(): I don't know how to handle files with mode 40755
I do not have any output and just returns to the command prompt.
@Firerat, I tried your script and it works but it does not list all the disk devices in computer
Here is the output:
Code:
# lsscsi
[0:1:0:0] disk LSI NWD-RLP4-1860 0002 /dev/sda
[1:0:0:0] disk ATA Corsair Force 3 5.02 /dev/sdb
[root@ Desktop]# ./test.sh
1) Quit
2) /dev/sdb1
Which device would you like to format?: 2
/dev/sdb1
1) Yes
2) No
Format another device?: 1
[root@ Desktop]#
Thanks I tired it but got an error on the output. It does now give me all the options. BTW is there a way to test this code with a command like fdisk -l with options for /dev/sda, dev/sdb in this particular case? I am sniffing thru the links you provided yesterday as well.
BTW i like your way much better then how I would have implemented. I understand the risk that this could easily destroy data on the boot drive.
I was thinking we can put a "are you sure you want to do this" in the comments
Code:
#!/bin/bash
RootFSDev=$( lsblk -o KNAME,type,MOUNTPOINT | awk 'if ($3 == "/"){sub(/[1-9]+,"");print $1}' )
Options=("Quit")
Options+=($(lsblk -o KNAME,type,MOUNTPOINT | awk '/disk/||/part/{if ($1 != "'$RootFSDev'" && $3 != "/" )print "/dev/"$1}'))
ShredSelect () {
PS3='Which device would you like to format?: '
select Disk in "${Options[@]}";do
case $Disk in
Quit)
break
;;
'')
echo "Please select option 1 to ${#Options[@]}: "
;;
*)
echo $Disk # replace with your commands
PS3="Format another device?: "
select YN in Yes No;do
case $YN in
Yes)
ShredSelect
return
;;
No)
exit
;;
esac
done
;;
esac
done
return
}
ShredSelect
Here is the out put it look like I am getting some kind of syntax error.
Code:
[root@ Desktop]# ./test.sh
awk: if ($3 == "/"){sub(/[1-9]+,"");print $1}
awk: ^ syntax error
1) Quit
2) /dev/sdb
3) /dev/sdb1
4) /dev/sdb2
5) /dev/sda
Which device would you like to format?: 5
/dev/sda
1) Yes
2) No
Format another device?: 2
[root@ Desktop]#
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.