LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 07-12-2014, 12:25 PM   #1
dreamqq
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Rep: Reputation: Disabled
shell scripting with fdisk, while loop with double [[ brackets errors


Hi

I'm new at shell scripting and I'm trying to list all usb devices and make the user choose from them to mount the device and image the usb with dd command. However, I'm having problems with the code.

1) while loop problem with the validation of the number of usb devices in the array( I want the while loop to not accept any number more or less than the listed usb mass-storage devices )

2) while loop with #!bin/sh cant accept double [[ and if I want to use one [ it gives me an error when I use =~ in this [[ ! "$usbNo" =~ ^[0-9]+$ ]] ( this condition is checking for input if it's letter or number )

3) problem with fdisk when I run the script on another machine. It tell me that the fdisk command not found. I want to use fdisk so that I can get the partition number for example sdb1 not sdb to mount the device later.

4) I want to list the usb mass-storage devices with their vendor name to make it easier for the user to select the device

P.S. I'm developing the script for a machine is running custom Linux OS with busybox. Therefore, I have to #!bin/sh not #!bin/bash and I run the script as a root.

Thank you.


Code:
#!/bin/sh

export USBev=$( ls -la /sys/block | grep usb | grep -o sd.  | uniq | tail -9 )
	
	 
	#List all the USBs and make the user to choose one of them

	 	i=1   
		for Upath in $( fdisk -l /dev/"$USBev" | grep -o '^/dev/sd[a-f][0-9]' ) ; do
		dev=$( basename "$Upath"  ) 
		devices="$devices[i]${dev}"
			
		echo " $i Usb Device $dev "  
		i=$((i+1))
		done

		#chech for the input validation	 
	        while  ! [[ "$usbNo" =~ ^[0-9]+$ ]] &&  read -p "Choose a USB device ?   " usbNo ; do
        	
                # ! [[ ${usbNo} -gt  ${i-1} &&  ${usbNo} -lt ${i-1} ]]
		export Upath="$Upath{devices[$usbNo]}"
		done
                
                echo " The device selected is: $Upath "
 
Old 07-12-2014, 01:00 PM   #2
Didier Spaier
LQ Addict
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-14.2beta2 on Lenovo Thinkpad W520
Posts: 6,778

Rep: Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115Reputation: 2115
2) The construct [[ is a bashism. To know the available constructs in sh type "man ash"
3) Permissions and/or path problem, or just binary not shipped. On most distributions fdisk is available only for root.
4) See lsusb and lsblk
 
Old 07-13-2014, 07:28 AM   #3
dreamqq
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Didier Spaier View Post
2) The construct [[ is a bashism. To know the available constructs in sh type "man ash"
3) Permissions and/or path problem, or just binary not shipped. On most distributions fdisk is available only for root.
4) See lsusb and lsblk
Thank you very much. I was learning Bash scripting and I didn't know that the device that I'm developing for can't run Bash, so I'm trying to fix it as much as I can.

About fdisk, on the device I'm root but fdisk doesn't work there, but on my machine it works fine maybe because the busybox. I need fdisk just to get the partition name of the USB. Also, I forgot to mention that the main problem with this code is in the array itself those lines can't run in Dash just Bash, could you tell me why it said Bad substitution. I didn't find it in the (man sh) or in the web.

Code:
devices[i]="${dev}"   # this line 

export Upath="${devices[$usbNo]}"   # and this line
 
Old 07-13-2014, 08:15 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by dreamqq View Post
I'm developing the script for a machine is running custom Linux OS with busybox. Therefore, I have to #!bin/sh not #!bin/bash and I run the script as a root.
Is it not possible to install bash in it? Most parts of your script is really better done with Bash most especially arithmetic (both evaluations and comparisons), and use of arrays.
 
Old 07-13-2014, 11:25 AM   #5
dreamqq
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by konsolebox View Post
Is it not possible to install bash in it? Most parts of your script is really better done with Bash most especially arithmetic (both evaluations and comparisons), and use of arrays.

I rebuild the system and activate the Bash shell and it works. However, this system is part of my dissertation work and I asked if I'm allowed to do that and I have been told is better to use the busybox shell because it will increase the size of the OS and it might not fit in the Bios.
Thank you anyway.
 
Old 07-14-2014, 11:56 AM   #6
dreamqq
LQ Newbie
 
Registered: Jun 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
I solved the fdisk issue by configuring the busybox and enabling it. However, the array problem still an issue. It said bad substitution and by googling stuff I found out that ash (busybox) doesn't support arrays, is there any way that I can list all the usb devices without an array or how can I change the the substitution from bash to ash ??
 
  


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
Cannot compile while loop in shell scripting kiki2009 Linux - Newbie 3 10-02-2012 09:14 AM
[SOLVED] Shell scripting - for...in loop and variables question kcleveland Programming 12 11-20-2011 09:17 AM
[SOLVED] Shell scripting loop error kathirvel Programming 5 07-28-2011 03:06 AM
Until loop in bash shell scripting help computergeek7 Programming 6 03-02-2010 12:34 AM
shell scripting basics.. how to forward double quotes ?? m_kane Linux - General 3 01-31-2009 12:45 PM


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