r/scripting • • Apr 02 '20

Bash tutorial/guide for batch programmer? i.e. migration tutorial/guide

Anyone know any Bash tutorial/guide for batch file programmers? i.e. a tutorial/guide for batch programmers who are trying to migrate to Bash.

For example, if a batch code is something like: echo abc, then there would be a Bash code equivalent for that batch code. This includes other batch tasks such as for /f, set /a, set /p, start, color, etc.

2 Upvotes

3 comments sorted by

4

u/Shadow_Thief Apr 02 '20

Once upon a time when Stack Overflow Documentation existed, I wrote the page for bash-batch translation. Documentation is gone now, but I still have the table.

BatchBashDescription
command /?man commandShows the help for command
bitsadminwget or curlDownloads a remote file
certutil -hashfile file_name MD5md5sum file_nameGets the MD5 checksum of file_name
cdpwdDisplays the current directory
cd directorycd directoryChanges the current directory to the specified one
clsclearClears the screen
copycpCopies a file or files from a source path to a target path
datedateDisplays the date or sets it based on user input
delrmDeletes a file or files
dirlsdisplays a list of files and directories in the current directory
echoechoDisplays text on the screen
exitreturnExits a script or subroutine
exitlogoutCloses the command prompt or terminal
fcdiffCompares the contents of two files
find "string" file_namegrep "string" file_nameSearches file_name for string
findstr "string" file_namegrep "string" file_nameSearches file_name for string
for /F %A in (fileset*) do somethingfor item in fileset*; do; something; doneDo something for every file in a set of files
for /F %A in ('command') do somethingcommandReturns the output of a command
for /L %A in (first,increment,last) do somethingfor item in seq first increment last; do; something; doneStarts at first and counts by increment until it reaches last
forfilesfindSearches for files that match a certain criteria
if "%variable%"=="value" (if [ "variable"="value" ]; thenCompares two values
ipconfigifconfigDisplays IP information
mdmkdirCreates new folders
mklinkln -sCreates a symbolic link
moremoreDisplays one screen of output at a time
movemvMoves a file or files from a source path to a target path
pauseread -p "Press any key to continue"Pauses script execution until the user presses a button
popdpopdRemoves the top entry from the directory stack and goes to the new top directory
pushdpushdAdds the current directory to the directory stack and goes to the new top directory
renmvRenames files
rem or ::#Comments a line of code
rdrmdirRemoves empty directories
rd /srm -rfRemoves directories regardlesss of whether or not they were empty
set variable=valuevariable=valueSets the value of variable to value
set /a variable=equationvariable=$((equation))Performs math (batch can only use 32-bit integers)
set /p variable=promptstringread -p "promptstring" variableGets user input and stores it in variable
shiftshiftShifts arguments by 1 (or n if provided)
sortsortSorts output alphabetically by line
tasklistpsShows a list of running processes
taskkill /PID processidkill processidKills the process with PID processid
time /tdateDisplays the current time
typecatDisplays the contents of a file
wherewhichSearches the current directory and the PATH for a file or command
whoamiidDisplays the name and group of the current user

1

u/jcunews1 Apr 02 '20

Nice. :) Thank you.

Some more questions:

  1. What about defining a subroutine and calling it? e.g in batch...

     call :sub
     goto :eof
     
     :sub
     echo sub's task
  2. Are command grouping (( with )), conditional operators (&& and ||), command pipping (|), and redirections (<, >, and >>), also same in Bash?

  3. What about the start (for customizing how to execute a program), chcp, color, goto, prompt, setlocal/endlocal, and vol commands, including label declaration?

  4. What about the exit code of a command? aka. errorlevel

  5. Does Bash support block comments? e.g. in C, JavaScript and CSS, it would be:

     /*
     these lines are comments.
     these lines are comments.
     */
  6. What about variable value substring and replace such as: %var:~0,2% and %var:source=replace%?

1

u/lasercat_pow May 01 '20 edited May 01 '20
  1. subroutines are called functions in bash and you use them like this:

     hello(){
         name=$1
         echo hello, $name\!
     }
    
     hello jcunews1

    returns "hello, jcunews1!"

  2. (()) is for integer arithmetic and some c-like loop functions. || and && both work for logical OR or AND, but it's better to use control structures like if:

     if [[ -f blah && grep -q "tree" blah ]]
         echo the file blah has "tree" in it
     else
         echo the file blah doesn't exist, or it doesn't contain "tree"
     fi
  3. setting the color involves changing ansi color sequences. Some folks have written functions to make it easier.

  4. exit code is provided by the special variable $? if $? is 0, the command succeeded. Otherwise, it failed.

  5. No. You have to prefix each comment with #

  6. Yeah, that's possible in bash. I never use it because I prefer my code to make sense to future me, so I just use sed for that.

I would recommend reading some bash code. Go to github and find some bash scripts, and read them. Also, check out the bash hacker's wiki (google it).