LinuxQuestions.org
Register a domain and help support LQ
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 02-02-2014, 06:28 PM   #1
steve51184
Member
 
Registered: Dec 2006
Posts: 379

Rep: Reputation: 30
can't do a few things in bash that i could in batch.. help?


recently moved from windows to linux and i have a script to convert from batch to bash

i'm doing ok so far (at least for the commands) but i'm stuck on a few things as i'm so used the whole batch "if THIS then GOTO that" way of doing things

for example what is the bash equivalent of these three things:

1. this one checks the folder 'c:\foobar' for files with the extension of '.foo' and '.bar' and if one is detected goes to ether :foo or :bar and runs the commands

Code:
if exist c:\foobar\*.foo goto foo
if exist c:\foobar\*.bar goto bar
goto EOF
:foo
[commands here]
goto EOF
:bar
[commands here]
goto EOF
2. this will check a variable '%~x4' in this case which is the variable %4 (a full file name with extension) but modified to only show the extension part

Code:
IF /I "%~x4"==".foo" GOTO foo
IF /I "%~x4"==".bar" GOTO bar
goto EOF
:foo
[commands here]
goto EOF
:bar
[commands here]
goto EOF
3. a bit like #2 but for folder names so it looks for the word 'foo' in the %~4 variable (a folder name)

Code:
echo "%~4" | findstr /i "foo" >nul 2>&1 && GOTO foo
echo "%~4" | findstr /i "bar" >nul 2>&1 && GOTO bar
goto EOF
:foo
[commands here]
goto EOF
:bar
[commands here]
goto EOF
would be awesome to know how to do this in bash
 
Old 02-02-2014, 06:45 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,331
Blog Entries: 55

Rep: Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531Reputation: 3531
I'll do just the first one and give you some pointers.

Quote:
Originally Posted by steve51184 View Post
this one checks the folder 'c:\foobar' for files with the extension of '.foo' and '.bar' and if one is detected goes to ether :foo or :bar and runs the commands
Code:
if exist c:\foobar\*.foo goto foo
if exist c:\foobar\*.bar goto bar
goto EOF
:foo
[commands here]
goto EOF
:bar
[commands here]
goto EOF
Depends on what processing you need. For example this could do anything like converting, copying, moving etc, etc:
Code:
find /some/path/ -type f -iname \*.foo -print0 | xargs -0 -iX doSomething 'X'
same difference:
Code:
foo() { doSomething; }
find /some/path/ -type f -iname \*.foo | while read ITEM; do
 foo "${ITEM}"
done
...same difference:
Code:
find /some/path/ -type f | case ITEM in
 *.foo) doSomething "${ITEM}";;
 *.bar) doSomethingElse "${ITEM}";;
esac

BASH intros:
http://www.gnu.org/software/bash/man...ode/index.html
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://www.tldp.org/LDP/Bash-Beginne...tml/index.html

BASH scripting guides:
http://mywiki.wooledge.org/BashGuide
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
# these are must reads so do read them!

Common questions / problems:
http://mywiki.wooledge.org/ParsingLs
http://mywiki.wooledge.orgDontReadLinesWithFor
http://mywiki.wooledge.org/UsingFind
http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

The Advanced BASH scripting guide:
http://www.tldp.org/LDP/abs/html/

Bourne shell (compatibility):
http://www.grymoire.com/Unix/Sh.html

Also see:
http://www.linuxquestions.org/questi...l-links-35334/
http://www.linuxquestions.org/questi...nd-line-32893/
http://www.linuxquestions.org/questi...ks-4175433508/
 
1 members found this post helpful.
  


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
A Personal BASH FFmpeg Batch Script (WIP) kDest Programming 9 04-24-2009 11:11 AM
need a bash script to batch convert .wav to .mp3 nass Slackware 15 06-23-2007 02:00 AM
Bash to Batch Delusionz Linux - General 1 05-14-2006 10:07 PM
Bash - Batch File Rename Help... emailarron Linux - Newbie 4 01-26-2006 08:35 AM
Batch Renaming in bash xushi Programming 6 07-07-2005 04:24 PM


All times are GMT -5. The time now is 05:21 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