LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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, 07:32 AM   #1
powerplyer
LQ Newbie
 
Registered: Mar 2012
Posts: 16

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 07:47 AM. Reason: forgot to add the shred command
 
Old 10-28-2013, 09:24 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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, 11:41 AM   #3
powerplyer
LQ Newbie
 
Registered: Mar 2012
Posts: 16

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, 12:15 PM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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, 12:25 PM   #5
schneidz
Senior Member
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 4,217

Rep: Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643Reputation: 643
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, 01:24 PM   #6
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,222

Rep: Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155
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 01:26 PM.
 
Old 10-28-2013, 02:14 PM   #7
powerplyer
LQ Newbie
 
Registered: Mar 2012
Posts: 16

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, 02:28 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
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, 02:56 PM   #9
powerplyer
LQ Newbie
 
Registered: Mar 2012
Posts: 16

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, 03:11 PM   #10
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 3,222

Rep: Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155Reputation: 1155
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, 03:56 PM   #11
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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 03:59 PM.
 
Old 10-28-2013, 04:16 PM   #12
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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 04:19 PM.
 
Old 10-28-2013, 06:20 PM   #13
powerplyer
LQ Newbie
 
Registered: Mar 2012
Posts: 16

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, 06:56 PM   #14
Firerat
Senior Member
 
Registered: Oct 2008
Distribution: Debian Jessie / sid
Posts: 1,471

Rep: Reputation: 444Reputation: 444Reputation: 444Reputation: 444Reputation: 444
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 07:38 PM.
 
Old 10-28-2013, 07:32 PM   #15
powerplyer
LQ Newbie
 
Registered: Mar 2012
Posts: 16

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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-31-2012 12:20 AM
[SOLVED] Perform an action on a list/array of variables from user input in bash eamesj Programming 1 12-30-2012 11:22 PM
Selecting preset variables by user input in a bash script Lateralus138 Programming 12 09-27-2012 03:33 AM
using read and setting variables from user input string tekgek Programming 1 11-27-2008 02:10 AM
changing variables using user-input Kai' Linux - General 3 04-29-2008 07:04 PM


All times are GMT -5. The time now is 09:02 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration