LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-28-2013, 06:32 AM   #1
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Rep: Reputation: Disabled
Using dd command with user input variables


Hello newbie here!!! (please be gentle)

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
 
Old 10-28-2013, 08:24 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
My question back is ... where are you stuck? Assuming the only question is on getting info from the user, try:
Code:
$ help read
 
Old 10-28-2013, 10:41 AM   #3
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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
 
Old 10-28-2013, 11:15 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
So you would like the user to enter in 4 variables??

What options are you referring to?

It would appear you will need to be clearer in your expectations as what you wrote initially is vastly different to your new script.
 
Old 10-28-2013, 11:25 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this seems like what the op wants ?:
Code:
#!/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
 
Old 10-28-2013, 12:24 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Maybe this script I made a while ago to auto-format SD cards for use in an embedded system would help (namely the question and answer bit):

Code:
#!/bin/bash

device=$1

if [[ -z "$device" ]]; then
   echo "No device provided, exiting"
   exit 1
fi

val=$(df | grep $device)
if [[ $? -ne 1 ]]; then
   echo "$device is currently mounted, please unmount before running this script"
   exit 2
fi

echo "Going to format $device, are you sure (y/N)?"
read ans
if [[ "$ans" != "y" && "$ans" != "Y" ]]; then
   echo "Exiting on user cancel"
   exit 3
fi

size=$(fdisk -l $device | grep bytes | head -n 1 | awk '{print $5}')
sizeGB=$(echo "$size / 1073741824" | bc -l | xargs printf "%7.2f\n")
echo "Detected a $sizeGB GB device at $device, is this correct (y/N)?"
read ans
if [[ "$ans" != "y" && "$ans" != "Y" ]]; then
   echo "Exiting on user cancel"
   exit 3
fi

cyl=$(echo "$size / 8225280" | bc -l | xargs printf "%d\n" 2>/dev/null)

dd if=/dev/zero of=$device bs=1024 count=1024

echo "128,130944,0x0C,*
131072,,,-" | sfdisk --force -D -uS -H 255 -S 63 -C $cyl $device

mkfs.vfat -F 32 ${device}1 -n boot
mke2fs -j -L rootfs ${device}2

sync

Last edited by suicidaleggroll; 10-28-2013 at 12:26 PM.
 
Old 10-28-2013, 01:14 PM   #7
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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:


Code:
lshw -class disk
*-disk
description: SCSI Disk
product: NWD-RLP4-1860
vendor: LSI
physical id: 1.0.0
bus info: scsi@0:1.0.0
logical name: /dev/sda
version: 0002
serial: 2693268167154132291
size: 1733GiB (1861GB)
capacity: 1733GiB (1861GB)
capabilities: 15000rpm
configuration: ansiversion=6 logicalsectorsize=512 sectorsize=512
*-disk
description: ATA Disk
product: Corsair Force 3
physical id: 0.0.0
bus info: scsi@1:0.0.0
logical name: /dev/sdb
version: 5.02
serial: 1231048400FF19690001
size: 55GiB (60GB)
capabilities: partitioned partitioned:dos
configuration: ansiversion=5 logicalsectorsize=512 sectorsize=512 signature=0005e2b8

Ask the user to select sda, sdb, sdc, sde,
Input from user would option of one of the above. In this particular case it would be sda.

Have the script run the
Code:
shred -vz -n 3 /dev/$sda sdb sdc sdd sde
(Only on one from user input)
Code:
dd if=/dev/$sda sdb sdc sdd sde  bs=64K count=1 | od –c
(Use the same user input)
sleep or pause for 5 sec.

I modified the original code a little, but I still think it is wrong.

Code:
#!/bin/bash
# rsync using variables
lshw -class disk
read -p "Please select the Nytro drive: Option include sda, sdb, sdc, sdd, sda.WARNING do not select the boot drive" sda sdb sdc sdd sde
echo "format starting"
shred -vz -n 3 /dev/$sda sdb sdc sdd sde 
dd if=/dev/$sda sdb sdc sdd sde  bs=64K count=1 | od –c
echo "Format and Shred complete"
sleep 5

again thanks all for the help.
 
Old 10-28-2013, 01:28 PM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Here is a question for you: Would you write the following line exactly as it appears here on the command line:
Code:
shred -vz -n 3 /dev/$sda sdb sdc sdd sde
If yes, what is the expected output and outcome?
 
Old 10-28-2013, 01:56 PM   #9
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
Here is a question for you: Would you write the following line exactly as it appears here on the command line:
Code:
shred -vz -n 3 /dev/$sda sdb sdc sdd sde
If yes, what is the expected output and outcome?
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
just as an FYI the shred - delete a file securely, first overwriting it to hide its contents

Please let me know if you have more questions and thanks for the help.
 
Old 10-28-2013, 02:11 PM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by powerplyer View Post
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:
Code:
shred -vz -n 3 /dev/sdc sdb sdc sdd sde
 
Old 10-28-2013, 02:56 PM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
a simple select script

easier to validate user input

Code:
#!/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,

Code:
  *) echo "Please select option 1 or 2: "

Last edited by Firerat; 10-28-2013 at 02:59 PM.
 
Old 10-28-2013, 03:16 PM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
thinking about it, rootfs may not be on sda

get the rootfs and remove it from the 'Options' array
Code:
rootfs=$(mount | awk '$3 == "/"{gsub(/\//,"\\/");print $1}')
Options=("Quit")
Options+=( $(mount | awk '/\/dev\/sd/ && !/'$rootfs'/{print $1}') )
Edit, simpler

Code:
Options=("Quit")
Options+=$(mount | awk '/\/dev\/sd/{if ($3 != "/")print $1}') )

Last edited by Firerat; 10-28-2013 at 03:19 PM.
 
Old 10-28-2013, 05:20 PM   #13
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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]#
 
Old 10-28-2013, 05:56 PM   #14
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian sid
Posts: 2,683

Rep: Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783Reputation: 783
the Options array was built from the output of mount and excluded the partition which was mounted as root (/)

well, actually the initial script was hardcoded to exclude sda ( sd[b-z] ) , sdb, c d .. to z

maybe you want,,
Code:
lsblk -o KNAME,type,MOUNTPOINT | awk '/disk/||/part/{if ($3 != "/")print "/dev/"$1}'
Warning:
that will still let you trash /
assume sda1 is root
it will be not be listed, however sda will, trash sda you trash sda1 also


Code:
#RootFSDev=$( lsblk -o KNAME,type,MOUNTPOINT | awk 'if ($3 == "/"){sub(/[1-9]+,"");print $1}' )
# edit ^^ broken
RootFSDev=$( lsblk -o KNAME,type,MOUNTPOINT | awk '$3 == "/"{sub(/[1-9]+/,"");print $1}' )
Options=("Quit")
Options+=($(lsblk -o KNAME,type,MOUNTPOINT | awk '/disk/||/part/{if ($1 != "'$RootFSDev'" && $3 != "/" )print "/dev/"$1}'))
you should adapt to avoid trashing anything you don't want to be trashed
maybe you only want/need partitions?


Warning:
lsblk will not be able to list mountpoint for things like btrfs, and possibly lvm and the like



Warning:
End of the day, this script is something that makes something dangerous too easy
keep that in mind


Last edited by Firerat; 10-28-2013 at 06:38 PM.
 
Old 10-28-2013, 06:32 PM   #15
powerplyer
Member
 
Registered: Mar 2012
Posts: 31

Original Poster
Rep: Reputation: Disabled
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]#
Thanks again, this is so much fun...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Defining variables/arrays in csh from stdn/user input eamesj Programming 5 12-30-2012 11:20 PM
[SOLVED] Perform an action on a list/array of variables from user input in bash eamesj Programming 1 12-30-2012 10:22 PM
Selecting preset variables by user input in a bash script Lateralus138 Programming 12 09-27-2012 02:33 AM
using read and setting variables from user input string tekgek Programming 1 11-27-2008 01:10 AM
changing variables using user-input Kai' Linux - General 3 04-29-2008 06:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration