LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-31-2012, 09:40 PM   #1
corstar
Member
 
Registered: Feb 2005
Location: Perth, Western Australia
Distribution: Manjaro Linux
Posts: 30

Rep: Reputation: 2
need to merge two scripts into one


Hi all.

I have two shell scripts to make a file executable, I would like to merge them into one. Not sure on the bash scripting syntax to use.

script1 - make .sh executable

for FILE in *.sh
do chmod u+x *.sh
done


script2 - make .exe executable

for FILE in *.exe
do chmod u+x *.exe
done

I'm not really sure on the 'if' 'else' 'then' usages.

Thanks
 
Old 03-31-2012, 09:53 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
try this one:
Code:
for FILE in *.sh *.exe; do
	chmod u+x "$FILE"
done
 
1 members found this post helpful.
Old 03-31-2012, 10:08 PM   #3
corstar
Member
 
Registered: Feb 2005
Location: Perth, Western Australia
Distribution: Manjaro Linux
Posts: 30

Original Poster
Rep: Reputation: 2
Thanks for the quick reply and help.
Thread marked as solved
 
Old 04-01-2012, 10:43 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'd like to explain the above in more detail, if I may.

The syntax of the basic for loop is this:

Code:
for variable in <wordlist> ; do
	commands
done
If the <wordlist> includes variables, brace expansions, command substitutions, or in globbing patterns, as in this case, they get expanded into their final values before the loop is run. This means you can include as many different entries as you want after the "in". What matters to the loop is how the list looks after expansion.

Code:
$ echo 'foo1 bar1 baz1' >testfile.txt
$ varA='foo2 bar2 baz2'
$ arrB=( foo3 bar3 baz3 )
$ touch {foo,bar,baz}4

$ for var in $(cat testfile.txt) $varA "${arrB[@]}" *4 ; do echo "$var" ; done
foo1
bar1
baz1
foo2
bar2
baz2
foo3
bar3
baz3
bar4
baz4
foo4

$ rm *4 testfile.txt
$ unset varA arrB

However, in spite of what I just posted, you should generally not read lines from text files or commands with for. Use a while+read loop for that. Only globbing patterns are completely safe from word-splitting effects.


Notice also that I did not quote the $(..) or the $varA, as I wanted the contents to be split into the individual words. Quotes would have caused the contents to be treated as a single value. Generally, it's much better to use an array for lists of words, as my use of arrB demonstrates.

Code:
$ for var in "$(cat testfile.txt)" "$varA" "${arrB[@]}" "*4" ; do echo "$var" ; done
foo1 bar1 baz1
foo2 bar2 baz2
foo3
bar3
baz3
*4
 
  


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
Merge many files in to one big file. like 20 file merge in one big file Jmor Linux - Newbie 2 10-29-2008 09:41 PM
LXer: kgdb, To Merge Or Not To Merge LXer Syndicated Linux News 0 02-05-2008 06:10 PM
LXer: KHTML Vs Webkit: To Merge or Not To Merge LXer Syndicated Linux News 0 10-27-2007 06:41 AM
convert and merge a fat32 to ext3 and then merge w/ another ext3? nkoplm Linux - General 3 03-23-2006 10:37 PM
VPN: Debian Scripts -> Mandriva 2006 Scripts Undefined Mandriva 0 11-30-2005 12:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:16 PM.

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