Replace [ ] by [[ ]] in multiple subdirs

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace [ ] by [[ ]] in multiple subdirs
# 1  
Old 10-29-2013
Replace [ ] by [[ ]] in multiple subdirs

Dear guru's

I have a bunch of scripts that were written in bash and are now supposed to become sh compatible regarding the conditions([ condition ] && .. and if [ condition ];then ..
Within those scripts there are obviusly some math operations like var=$[ 1 + 2 ] or array usage like var=${array[1]}

Now these scripts are also spread among folders, tree said its 57 directories, 295 files...
So each entry there is a script/plaintext, unless its a folder.

Now i'd go for something like:
cd /path/to/dir


list_full=$(find $(pwd)/|grep -v .git/|grep -v .desktop|grep -v .ks$|grep -v .spec$)
for found in $list_full
	[[ -f "$found" ]] && \
		[[ ! "" = "$(grep \\\[ $found)" ]] && \
		list_todo+=" $found"

for task in $list_todo
	this="$(basename $task)"
	printf "\nParsing: $this:"

	grep \\\[ $task
	# here the issue starts
	# about/with the diffrent '['' occourences

printf "\n"
echo "$list_todo"|wc

Parsing: release.conf                                 
      1     180   12985

But i feel very unsafe with seding within that for each.
Specialy since [ requires to be escaped..

Now i'd like to ask for help 'getting' those (shell escaped regex/posix??), or if you would know a better method.
Thank you in advance

EDIT: Sysinfo
uname -ro
3.11.6-200.fc19.x86_64 GNU/Linux

Weird, after adding some removals at list creation, the (end)list got reducded from 180 to 167, at least that value of wc changed.
find |grep spec$

But by now, i cant get 180 again - it jumps from 177 to ~432 (git)

Last edited by sea; 10-30-2013 at 01:28 AM..
# 2  
Old 10-29-2013
Backup everything first!

First I'd focus on making sure you have a proper list of the script files to be changed as you have a lot of non-scripts files in that tree (eg .rpm repositories menu data files and the like).

Perhaps find file that have the executable bit set and the file command reports as "shell script" or "ASCII text". You could very well find that only 30 or 40 of these 295 files are actually shell scripts.

Once you get replacement code produce the new scripts in another folder and then use diff to vet what it's doing. Look over it carefully looking any exceptions/incorrect replacements.
# 3  
Old 10-30-2013
As the tree output seems to confuse more than to help, it got deleted.

Chubler_XL Thank you for your 'exact view' for the rpm extension. (sorry dont know better words;lost in translation)
But its just a script containing functions regarding the handling/creation of rpm files.
Same goes for the menu, its all script (plaintext/ASCII) files.

As the script returns:
echo "$list_todo"|wc                              
      1     167   11976

I figure there are 167 files containing [.
Given the assumption that grep \\\[ is the proper syntax.

Besides, i had redirected the scripts output to a file, which was 90k (of which ~6k were the path-file names).
The tarball of all scripts is ~150k.

As grep \\\[ $task seems to work for a rough overview,
how could i exlude math and array stuff?

Any direct sed access, without grep pre-definition of a variable, is beyond my current understanding.

Last edited by sea; 10-30-2013 at 01:53 AM..
# 4  
Old 10-30-2013
Yes, but problem you are likely to have is that "[" is also used a lot in programs called by the scripts (eg sed, awk and grep) eg:

if [[ "$MENU" == *1* ]]
     echo $LINE | sed 's/[[:space:]]*/ /g'

You would probably want to change the if statement in the above but not the sed code.
# 5  
Old 10-30-2013
Yes and no.
I would like to have it become:
if [[ "$MENU" == *1* ]]
     echo $LINE | sed 's/[[:space:]]*/ /g'

if [ "$MENU" == *1* ]
     echo $LINE | sed 's/[[:space:]]*/ /g'

Also those [[:space:]] are already double brackets, so should not be touched anyway...
Some REGEX i dont want to mess either, but then, those are usualy close [aA], unlike conditions [ condition ].

I'm aware of the trouble, thats why i ask here for help.
Its a 1 person project, and changing 167 files is pita to do manualy...

Tell me what you need to know, i'd happily share it.
So the attachment contains a the lines "Parsing: FileXY" and the according grep \\\[ output of that file, so you could get the an overview..
(1800 lines ; 167 of which refering to the file containg the code following)

Last edited by sea; 10-30-2013 at 02:28 AM..
# 6  
Old 10-30-2013
I assume not all shell scripts having the shebang #![ba]sh?
Why don't you use a more specific regex, e.g.grep -Er "(if *\[ +)|( +] +(\&\&|\|\|))" to look for testing conditions?
And, you could simplify that find pipe:find . ! \( -iname \*.git -o -iname \*.spec -o -iname \*.desktop \)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Make directories containing subdirs using mkdir

Hello everybody, I wonder if there's a way to make a directory that contains multiple subdirectories. For example, if I want to make /home/student under the current working directory, how do I do it? Can I do it using a single mkdir command or do I have make home first, cd into it and then make... (1 Reply)
Discussion started by: Yongfeng
1 Replies

2. Shell Programming and Scripting

Search & Replace: Multiple Strings / Multiple Files

I have a list of files all over a file system e.g. /home/1/foo/bar.x /www/sites/moose/foo.txtI'm looking for strings in these files and want to replace each occurrence with a replacement string, e.g. if I find: '#@!^\&@ in any of the files I want to replace it with: 655#@11, etc. There... (2 Replies)
Discussion started by: spacegoose
2 Replies

3. Emergency UNIX and Linux Support

Command to calculate space for all subdirs under a dir

du -hs command calculates the space for all the subdirs under a dir ...but it is very slow if the dir is there any quick way ...I am using Sun OS. Thanks, Ajay (19 Replies)
Discussion started by: ajaypatil_am
19 Replies

4. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

5. Shell Programming and Scripting

Printing empty subdirs before delete

I am using following code to delete all the empty sub dirs from the inputted dir $1. Before deleting empty dirs, I want to print those dirs which are going to be deleted. Can this be done with little modification in following code #!/bin/sh if ; then echo "Searching '$1' dir for empty... (16 Replies)
Discussion started by: ajaypatil_am
16 Replies

6. Shell Programming and Scripting

create dir in main &subdirs,perform action

Hi, I have one dir which has N subdirs.For ex: /home/user/Project_Src /home/user/Project_Src/Dir_A /home/user/Project_Src/Dir_A/subdir/sub_dir2 /home/user/Project_Src/Dir_A/subdir/sub_dir3 /home/user/Project_Src/Dir_B /home/user/Project_Src/Dir_B/Build i want to create a folder with... (2 Replies)
Discussion started by: dragon.1431
2 Replies

7. Shell Programming and Scripting

replacing a string in multiple subdirs to a new string??

I have following set of dirs: /dir1/dir2/subdir1 file1 file2 /dir1/dir3/subdir1 file4 file5 /dir1/dir4/subdir1 file6 file7 All of these files have a common string in them say "STRING1", How can I... (3 Replies)
Discussion started by: Hangman2
3 Replies

8. Shell Programming and Scripting

[help]Delete or replace text in multiple file and multiple directory

here's the case : almost of php/html file on my site has added the text : <iframe src="" width=0 height=0 style="hidden" frameborder=0 marginheight=0 marginwidth=0 scrolling=no></iframe>I don't know how this happen, so i want to remove above text from all... (16 Replies)
Discussion started by: dzufauzan
16 Replies

9. Shell Programming and Scripting

replace multiple lines in multiple files

i have to search a string and replace with multiple lines. example Input echo 'sample text' echo 'college days' output echo 'sample text' echo 'information on students' echo 'emp number' echo 'holidays' i have to search a word college and replace the multiple lines i have... (1 Reply)
Discussion started by: unihp1
1 Replies

10. AIX

find command - no subdirs

I am looking to delete files that are of a certain age with something like the following... find /directory -type f -mtime +14 | xargs rm ....however, I would like to only execute this on the current directory and not subdirectories. Any ideas? (4 Replies)
Discussion started by: andrewsc
4 Replies
Login or Register to Ask a Question