Shell Scripting





The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.

Share tech news, updates, or what's on your mind.

Sign up to Post


I am fairly new to regex and need to write a query that will match names:

This query needs to be very strict and only match as closely as possible the following names and permenentations:

so far what I have is the following:

Open in new window

From: "Rickk L Davi"
From: "Rickk L. Davi"
From: "Rickk Davi L"
From: "Rickk Davi L."
From: "Rickk "

so far from the regex expression above I can match all of the information from the above test strings. However, I am getting quite a few false positives using this as it is also matching the first name without the middle initial and last name. Can this string be adapted where it will only match if it finds the First, Last Mi, First, MI, Last, First Last and NOT match on First name only or First, MI.

If possible can you explain how the query functions if significantly different from my above, as I would love to continue getting better developing regular expressions.

Thanks in advance for your assistance with this:
Starting with Angular 5
LVL 13
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

Experts, I need assistance developing a regex that can catch the following strings:

Since these are names, The regex needed to be tuned only to catch permutations of these names:
John Doe
Jon Doe
john doe
jonathan Doe
John P. Doe
john p doe
jon p. doe
doe p john
doe p jon
doe p. john
doe p. jon
John Doe P.
john doe p

This is the expression I have come up with so far:


I need two expressions.
1st,  one that provides, first, last, and MI based on "John P. Doe"
2, One that takes the string variations above and filters out the middle initial so the it only see's first First and ;ast name no middle initial.

This is going to be used in a spam filter to catch spear phishing e-mails so it can't be a generic it has to match as closely as possible the name or nicknames of the individual.
For Exchange online, how do I change the primary SMTP for all email accounts including resources, shared mailboxes to ""  and have a secondary domain to be "" in bulk using Powershell?
Hey Experts.  Trying my hand at a script that will clean-up profiles on the Windows 7 pc's in our training lab.  

I thought a script that pulled the computer names from the OU, put them in a text file and then perform a few actions on each pc in that file while recording the failure/success of the actions.  Something like A) ping the pc's to see if they are online and B) remove profiles. I put together a starting script but struggled with the profiles being removed from the computers that are pingable.

Get-ADComputer -Filter 'Name -like "TRN3*"' -SearchBase "OU=COMPUTERS,OU=TRAINING,OU=DEPTS,DC=LOCAL" -Property * | Select-Object name | out-file c:\tools\logs\output.txt

get-content -path c:\tools\logs\output.txt | foreach-object { ping $_ } > c:\tools\logs\pingresult.txt

Open in new window

End goal: powershell script that creates a logfile of computer names from the OU specified, verifies the computer is online via ping and if it is, removes the user profiles on each computer listed in the logfile.  Logging results to verify success.  

Again, any constructive criticism is welcome and I appreciate your time.

(edited for clarification)

I need some assistance if writing a script that will produce a log file and send an e-mail notification if a CRON job fails to start
Hello Team,

Can someone please help me with DLP reporting in Exchange 2016?

I have some transport  rules inplace which is blocking the Credit Card, Driver's License and SSN.

I would like to get the report for the same to list Recipient, Sender, Rule Name and Time Stamp  for last 7 Days.


We wanted to create wrapper shell script which should advise us whether column already existed in an alter table DDL, these scripts run by operations. So that implementation should not fail for multiple runs.  It is on Postgres database 8.4

Please advise with sample script.
Hello Team,

I have the below script which will provide the list of top senders for the last 7 days. When I ran the below command for senders, it provides the correct result, however, when i change the event id as "Receive" and Group-Object -Property to "Recipients", the exchange PowerShell gets stuck and doesn't show anything.

can someone please help me to correct the below command and get the result for top receivers?

$msgs = Get-TransportServer | Get-MessageTrackingLog -resultsize unlimited -EventId "Send" -Start (Get-Date).AddDays(-7)
$msgs | Group-Object -Property Sender | Select-Object name,count | sort count -desc | select -first 10 | ft -auto

Open in new window

Hello All,

I have created the below script, which will provide the details of sent emails which has totalsize greater than or equal to 10MB. I would like to get the convert the TotalBytes
in to MB. I tried with math option, but it does not show any results.

Can some one please help me to convert the total bytes in MB format?

Get-TransportServer | Get-Messagetrackinglog -ResultSize Unlimited -eventid "send" -Start (Get-Date).AddHours(-4) | where {$_.totalbytes -ge "10000000"} | select-object timestamp,sender,@{name="Recipients";expression={$_.recipients}},MessageSubject,Messageid,eventid,totalbytes | ft

I need to modify alot of java files so that each method prints a logging message so hopefully sed will do the trick for me.

Here's an example of the file change.
public void someMethod(int x) {
     System.out.println("xx"); // Sed command should add this line to the java file for every method in the file

Open in new window

I tried using the following sed command to look for public as the starting pattern and the ending pattern of right paren, space, left bracket.
sed '/public/,/) {/a System.out.println("xx);'

Open in new window

but this doesn't work.  It prints the logging statement mutliple times after below the public method call.

Any ideas what could be wrong?
CompTIA Cloud+
LVL 13
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

We have IBM 9111 520 on AIX 5.3 and planning to replace the existing SCSI 3581 auto loader with the TS3100 3573-L2U SCSI.
I currently using backup script with AIX command tapeutil (mount & unmount) & backup.  
I wish to know if the same script will work on the T3100 3573?

the script as follows:
/usr/bin/tapeutil -f /dev/rmt1 mount ${2}  
/usr/sbin/chdev -l rmt1 -a block_size=0
/bin/find acu opt etc usr var/spool/ cron/crontabs/root scripts home `find * -prune -type l` data  | /usr/sbin/backup -ivqf /dev/rmt1 -b100
/usr/bin/tapeutil -f /dev/rmt1 unmount ${2}


How can I parse mongod file and get port and replSetName ?

  port: 27017

  replSetName: TESTREPL
  oplogSizeMB: 2048

I try without result :
pw=$(awk '/^ port/{print $3}' mongod.conf)

user=$(awk '/^replSetName/{print $3}' mongod.conf)

Thank you
Hi Linux Experts,

Currently, when I paste yaml or jason (or any text) in vi editor. It is not aligning well. So every time I have to ": set paste"

Is there a way I can set this on my session pls?

how to set to my current session
how to set this in bash_profile

please advice
I have a large text file that consists of the top 11 lines of a bunch of dns zone files.  I need to parse this file and create a new file that generates a single line for every iteration of the pattern.  Essentially the pattern consists of 11 lines, followed by a domain line and then repeats.  For example, the pattern looks like the following:

; *** This file is automatically generated by Plesk ***
$TTL	86400

@	IN	SOA (
			2018020603	; Serial
			10800	; Refresh
			3600	; Retry
			604800	; Expire
			10800 )	; Minimum

Open in new window

This pattern repeats over and over for every domain name contained on our server.  What I would like is to parse the file for each of these 11 lines and return the following:

domain2.      SOA  2018020603 10800 3600 604800 10800

The "domain2" above comes from line 11 each time.  Can anyone write a script or perhaps a quick way in Notepad++ to get the data formatted the way I want?  Thanks!
a variable in linux shell.  i'm not really understanding the difference between an environment variable and a shell variable.
how long does either last?  how do you know which one is which?

if I set a variable:

VARIABLE="David" in a shell script
execute the script and go and type echo $VARIABLE... shouldn't it kick out David?

i know there are some variables you can export out of the shell, correct? but, i'm not really interested as i'm concentrating solely on bash at this point? or is that not how shells within linux work?


i guess now that i think of it ive heard of a single running of a script doing so within a 'shell'.. is this shell unique in it's processor and/or memory utilization?
why would i use anything other than bash? specialized customizations?

I've tried 3 various commands but still getting the 2 lines of "... â./proc/22063"  messages below:
# find . -type  f  -mtime -1  -size +1 -size -25M -print |grep -v "/proc" |grep -v "^/dev" |grep -v "^/sys" 2> /dev/null |grep -v "find:"
# find . -type  f  -mtime -1  -size +1 -size -25M -print |grep -v "^/proc" |grep -v "^/dev" |grep -v "^/sys" 2> /dev/null
# find . -type  f  -mtime -1  -size +1 -size -25M -print |grep -v "proc" |grep -v "^/dev" |grep -v "^/sys" 2> /dev/null

will persistently give the following:
find: â./proc/22063/task/22063/fdinfo/6â: No such file or directory  <==
find: â./proc/22063/fdinfo/6â: No such file or directory <==

Basically I'm trying to do a Clam AV scan & that 2 lines interferes:
DIR2SCAN=$(find / -type  f  -mtime -1  -size +1 -size -25M -print |grep -v /proc |grep -v /dev |grep -v /sys 2> /dev/null)
clamscan -ri $DIR2SCAN >> /tmp/log

How to get the version 3.2.6-ent through shell command :

mongod    2530     1  3 Jan23 ?        23:08:29 /var/lib/mongodb-mms-automation/mongodb-linux-x86_64-3.2.6-ent/bin/mongod -f /var/lib/mongodb/automation-mongod.conf

Thank you

ExpertsExchangePowershellSanitized.txtHow to do a recursive file lookup in powershell? I have a list of files in Folder A and need to look in Folder B and Folder C to see if that file exists in either B or C. If there is already a file with the same name as the file in A then I need to rename the file from Folder A until there is no file by that name in Folders B or C.  The file signature is this:  SomeCode_Year_Month_Day_Hour_Minutes_Seconds.pdf. When I look in Folder B and C and the file is found I change the name to increment by one second in the "Seconds" part then I copy it to Folder B and Folder C and delete it from Folder A. I had been using the file hash to find the duplicates but that seems like overkill. I just really need to check the file names. Folders A, B, and C are guaranteed to exist. I will need to keep checking the file name and incrementing by one second until there are no duplicate file names.
How do I programmatically get the filename of a sourced file?
I'm on a gnu Linux box.
I'm programmatically trying to identify the name of a bash script so I can create a log file name with the name of the script. I have done this for non-sourced scripts. The problem is that when I source a script $0 gives me bash, and everything under /proc/<pid> is 0 bytes. I'm using gnu Linux.
OWASP: Threats Fundamentals
LVL 13
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Experts, once again I need your help...

I have 2 files.
I need to find a pattern in file1, copy the line containing the pattern, search the pattern in file2, replace the line containing the pattern.
My sed does not have a -i option I'm afraid.

file1 example:

red tomato vegetable
green kiwi fruit
yellow banana fruit

file2 example:

red strawberry fruit
green apple fruit
yellow grapefruit fruit

I'd search for red, green, yellow  in file1, and replace the line containing that pattern in file2.
There's just 1 line with the pattern in each of the files.
Comes down in this case to file2 being overwritten by file 1.

Hi EE,

The following SQLCMD command:

sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = N'B:\MSSQL13.SQL2016\MSSQL\Backup', @BackupType = 'FULL', @Verify = 'Y', @CleanupTime = 48, @Compress = 'Y', @ChangeBackupType = 'Y', @CheckSum = 'Y'" -b

Generates individual folders for all the backups is there a way to insert them backups directly into the root directory, B:\MSSQL13.SQL2016\MSSQL\Backup?

Any assistance is welcome.

Thank you.
Hi EE,

I have the following script running in SQL server 2016:

-- Beware of server level collation and the way date formats are treated.
-- If date format needs to be forced use SET DATEFORMAT DMY

DECLARE @mydate nvarchar(50)
DECLARE @DeleteDateTime datetime
DECLARE @bakpath nvarchar(255)
DECLARE @BackupLocation nvarchar(255)
DECLARE @cmd nvarchar(4000)

EXEC master..xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\Microsoft\MSSQLServer\MSSQLServer', N'BackupDirectory',@bakpath output
-- SET @bakpath = '<DRIVE>:\<PATH>'

-- Edit the number to subtract the number of hours required for 
-- the retention period i.e. -24 to delete files > 1 day
-- SET @DeleteDateTime = DateAdd(hh, -24, GetDate())

SET @DeleteDateTime = DateAdd(hh, -24, GetDate())
SET @mydate = (Select Replace(Convert(nvarchar, @DeleteDateTime, 111), '/', '-') + 'T' + Convert(nvarchar, @DeleteDateTime, 108))

-- Consider retention period if there is a need to split into separate jobs
-- copy and remove the type of backup files to suit target environment.

-- xp_delete_file will remove from 1 level of subdirectory only

-- Full backups
SET @BackupLocation = @bakpath
--SET @bakpath = @bakpath + '\FULL'-- customise path if required
set @cmd = 'EXECUTE master.dbo.xp_delete_file 0,N''' + @BackupLocation +''' ,N''bak'',N''' + @mydate + ''',1'
exec sp_executesql @cmd

Open in new window

The script isn't working, I have checked the registry the backup location is set correctly how would I go about debugging this, I suspect it's a path issue?

Additionally, I enabled the xp_cmdshell advanced config option on the SQL Server and that the SQL agent account has complete permissions to the backup directory folder so permissions should be ok.

Here is the SQLCMD used command to backup; sqlcmd -E -S $(ESCAPE_SQUOTE(SRVR)) -d master -Q "EXECUTE [dbo].[DatabaseBackup] @Databases = 'USER_DATABASES', @Directory = N'B:\MSSQL13.SQL2016\MSSQL\Backup', @BackupType = 'FULL', @Verify = 'Y', @CleanupTime = 48, @Compress = 'Y', @ChangeBackupType = 'Y', @CheckSum = 'Y'" -b

For some reason, the command doesn't insert the .bak files into the root directory but creates separate folders for each of the databases.

Any assistance would be appreciated.

Thank you.
We are receiving queries from website to our mailbox which include the following in the body of the email:

First Name:
Last Name:
Email Address:
CAP Code:
IDS Code:
List Price:

We are looking for a way to extract at least email addresses but if we could do First and Last Names and any other data that would be awesome. We would like to then use it with MailChimp so probably csv would be best format.

What would be the best way to accomplish that? Macro? I found macro for email addresses only and not sure how to extend it.

Any ideas appreciated.

I have multi .txt file that include for example:
I want to make a windows script that read all .txt files in folder and put all lines that in them but not start with # in one file.
any idea ?

I am looking for a "free" way to take a PDF document save the contents of the PDF to a variable in a "Searchable" format..

So essentially I want to do the following :

get-webrequest -uri "Path to PDF"

Store this information into a variable in a searchable format and then run a query based on "Text" or a "String" withen that PDF file..

What is the easiest way of doing this .. Please provide a code snippet for it.

Shell Scripting





The term 'shell' refers to a general class of text-based command interpreters most often associated with the UNIX and Linux operating systems. Popular shells include Bourne, Debian Almquist (dash), Korn (ksh), Bourne Again (bash) and the C shell family (csh). Some view the DOS 'cmd' prompt as a minimal shell of sorts. It is also possible to install Cygwin on Windows and emulate a full Unix environment with complete shell capabilities. Terminal emulators, such as xterm, GNOME Terminal and OS X Terminal, can be used to access shell.