[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More


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 have a file similar to the below example:


In a bash script How can I 'for' loop from the last item to process an action.
Exploring ASP.NET Core: Fundamentals
LVL 12
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

I'm running a scheduled task that runs a PowerShell script that's using a get-content user data (Alias) from CSV to make changes to those mailboxes. Is there a way to have the script spit out 2 text or csv files one with the mailboxes (aliases) that were successfully changed, the other with the failure mailboxes (aliases)?  

Dear Experts,

I hope you can help me with this manipulation of a file which needs to be re-designed so that it can be used in Graphviz.

Input file as below, chunk starts with "SCHEDULE" and ends with "END".
In between chunks there's a blank line.
In each of the chunks there is upper and lower half seperated by a ":".

In the upper part each line that start with "FOLLOWS" should come on 1 line with and preceeding "SCHEDULE", seperated with "->", double quotes are necessary...

In the lower half each line should be preceeded by the "SCHEDULE" line (the first line of the chunk)
                example: "SCHEDULE FTA_BES_ACC#SBP_BE_BOOT_J2P0"->"FTA_BES_ACC#SBP_BE_BOOT_J2P0"

Input file:              


                FOLLOWS FTA_BES_ACC#SBP_BE_BOOT_J2P0.@

When we get threat intels info for hashes to be added to Trendmicro
Officescan, the MD5 or SHA1 hash value has to be entered into an
IOC Editor (we use Mandiant's ie
 to generate 2 values indicated by 1st value & 2nd value enclosed in "...'  below.

However, sometimes, the intel that comes in can be more than a hundred, so this makes
it very tedious to manually generate the values & populate into the IOC file for Officescan
to read in.  Is there an automated way / script to generate the 2 values for each hash &
auto-populate (using script etc) or an easier way for Officescan to read in just the hash
values?    Have logged a case with Trendmicro & was advised to do it manually which
does not help at all.

attached the full IOC file.

   Sample IOC file's content:
<?xml version="1.0" encoding="us-ascii"?>
<ioc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" id="2146113a-1513-4be6-b07e-f43969847a6a" last-modified="2018-12-02T02:19:17" xmlns="http://schemas.mandiant.com/2010/ioc">
  <links />
    <Indicator operator="OR" id="a1c825b0-ae7f-4461-85dd-25a20720acac">          <== 1st value enclosed in "...";  once only for entire IOC file
      <IndicatorItem …
Hi ..
I booted from an bootable media and trying to chroot the root file system on the local drive to run a bash script (bscript.sh) in /usr/local/bin/ with 3 parameters which are files inside the /usr/local/bin/
i made a bash script as below:
mkdir /mnt/s1
mount /dev/sda1 /mnt/s1
chroot /mnt/s1 ./etc/local/bin/bscript.sh $(cat paramfile1) $(cat paramfile2) $(cat paramfile3)
But I get the error:
cat cannot find file paramfile1
cat cannot find file paramfile2
cat cannot find file paramfile3

/bin/bash: line 1 file not found

Open in new window

The bscript.sh runs without the parameters, since 'cat' cannot find the files that in the same directory as the bscript.sh (of course full path works with cat like:  cat /mnt/s1/....)
Could someone help to correct the above procedures.

Thank You for your Quick Prompts.

i started new for python, i would like know the equivalent python code for below shell script.

while read n
        curl -u admin:admin@123 -X GET https://jira-dev.abc.com/rest/api/2/issue/$n$data | /usr/bin/jq '.id' >> isad.csv

done < filename

where filename contains

Hi ...

Trying running a python script that calls a bash script as sudo.  to get all the standard input/output using the below:
sas_cmd = ['sudo', 'apacheart']
proc = subprocess.Popen(sas_cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

Open in new window

it is not calling the script at all ....
But, if i do the following:
proc = subprocess.Popen(['sudo','/usr/bin/apacheart'])

Open in new window

It calls the script.
Can someone please help to get the std/stout.
I am attempting to silently run an uninstall script via a GPO when a normal domain user logs into the device.
My understanding is that anything run at startup uses the system account and therefore the script should work fine but that is not my experience. The files copy over fine and the script runs but not with admin (install) privileges.

Any assistance to resolve this is appreciated

(See attached screenshots)

CMD file being executed:

@echo off
if not exist "C:\Programdata\install_wim_tweak.exe" xcopy "\\maccrayhs\msiapps\uninstall_edge\*.*" "C:\Programdata\"
echo Uninstalling Microsoft Edge...
cd /d "%~dp0"
echo Uninstalling Microsoft Edge...
C:\Programdata\install_wim_tweak.exe /o /l
C:\Programdata\install_wim_tweak.exe /o /c Microsoft-Windows-Internet-Browser-Package /r
C:\Programdata\install_wim_tweak.exe /h /o /l

Open in new window

I have a bash script with 8 positional parameters. each needs to be set from a file. So, I have 8 files with only one line each.
what is the best way to read each file and pass it to the bash script.
Thank you for your prompt help.
This is a php script, which basically does the send mail function via terminal shell, I am studying it and the curious thing is that it worked in my tests only in the Ubuntu 14.04 versions with php5 and postfix. But I could not get it to work on Ubuntu 18.04 or Debian 9. I would like to ask for help to change the code in order to make it work in those versions. Thank you.

Fundamentals of JavaScript
LVL 12
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.


Need a unix script to loop through thousands of folders and write a file listing the folders that contain less than x amount of subfolders.

Thank you.
I'll need a Shell (Bash) script (rather an exact command) that outputs

a)  files' name in the Solaris system, one file per line in the UNIX systems
b) that were modified/created the last 1470 minutes
c) exclude FIFO files, symbolic links, sockets (ie *.sock)
d) names of files of between 1 byte to 20MB in size
e) files in /dev, /devices, /kernel, /cdrom, /platform, /proc, /net
f) files mounted on NFS

I have about 1million files so hoping the command/script of outputting
the file can complete in 30mins, so may need efficient coding.

I need to amend the following script to read (ie for AV to scan) the above output file:

LOGFILE="/var/log/clamav/`hostname`-$(date +'%Y-%m-%d').log";
## suggest to change dirs below to root but exclude databases
DIRTOSCAN="/var /opt /home /etc /tmp /export";

for S in ${DIRTOSCAN}; do
DIRSIZE=$(du -sh "$S" |grep -v "/proc" |grep -v "/dev" |grep -v ...  /2>/dev/null | cut -f1);
## add to grep -v for any other file types to exclude

echo "Starting a daily scan of "$S" directory.
Amount of data to be scanned is "$DIRSIZE".";

clamscan -ri "$S" >> "$LOGFILE";
Dear Experts,

I am thinking to use SSIS to do the following procedure.

(1) copy many files to Linux machines. then
(2) execute a Linux program (command) to analysis those files. The program will output the result to the local Linux drive
(3) when (2) is done, then pull the result back to Windows machine

For (1) I am thinking of FTP task (by FTP or SFTP)

how about the task of (2) and (3) ?  Could you please help ... Thanks
Linux. I need to set up a script that I can run via cron in the middle of every night to copy / back up specific folders into another directory. I want all folders, files and permissions and attributes copied. The goal is to have a 24 hour old copy that I can restore in case a user screws up their files. Example:

/usr/home/thisuser/web_master would be copied / backed up to /usr/home/mainuser
/usr/home/thisotheruser/web_master would be copied / backed up to /usr/home/mainuser
/usr/home/andanotheruser/web_master would be copied / backed up to /usr/home/mainuser

Each night, the copy / back up would overwrite the one from 24 hours ago.

How do I set this script up, and what type of file extension should the script be?
I'll need to monitor several "privilege escalation related" Solaris 10 & RHEL6 files using
ACLs (Access Ctrl Lists) :

a) /etc/group, /etc/sudoers, /etc/cron.daily (or .weekly or any crons owned by root):
    ACL to send to syslog (so that we can pipe to SIEM) when permissions, ownership
    or contents of the above files are changed

b)visudo, sudo, usermod, useradd    command binary files :
   when these are being executed/run, ACL to send to syslog (who & when it's being

Appreciate an exact  setacl (or the actual commands/settings in RHEL6 & Solaris 10
x86  samples
I am trying to get a specific pid and ONLY that pid, not others that might have that pid embedded in them, i.e.


I only want to get the 345.  If they would let me use Perl it would be easy, but I have to use ksh.

Any ideas?

Batch scripting


I have run across an old dos batch script that utilizes something I have never seen.

The script is used to copy a file, but the reasons behind and the uses for the [ ] is what I am looking for.

copy h:filename.txt i:[Spreadsheet]\folder1\folder2

Open in new window

Hi Expert,

I have a SFTP job which is failing while transfer file for source to dest using "put" command.  As put command only works for single file.

Here my requirement is i want to transfer all the files available in "source/dir1/dir2" and want to merge them into single file then wanna SFTP.

Note:The script should also need to check how many files are there in the source directory before merge them!

thanks in advance.
Hi Expert,

How Bundle XML files together into a ZIP using shell script.

Your Comments will be really Appreciate.

Learn SQL Server Core 2016
LVL 12
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Need to harden a Solaris 10 that is connecting to Internet  from DMZ.

Anyone has a Solaris 10 hardening script that once run will harden for
a) Level 2 Profile
b) "Scored"

The attached which I got from GitHub doesn't seem quite fit to what's needed
& with all the "printf ...", it's more of listing out than actually doing hardening.

From CIS benchmark:

Scoring Information
A scoring status indicates whether compliance with the given recommendation impacts the assessed target's benchmark score. The following scoring statuses are used in this benchmark:
Scored  <==
Failure to comply with "Scored" recommendations will decrease the final benchmark score. Compliance with "Scored" recommendations will increase the final benchmark score.
Not Scored
Failure to comply with "Not Scored" recommendations will not decrease the final benchmark score. Compliance with "Not Scored" recommendations will not increase the final benchmark score.


 Level 1
Items in this profile intend to:
o be practical and prudent;
o provide a clear security benefit; and
o not inhibit the utility of the technology beyond acceptable means.
 Level 2  <==
This profile extends the "Level 1" profile. Items in this profile exhibit one or more of the following characteristics:
o are intended for environments or use cases where security is paramount
o acts as defense in depth measure
o may negatively inhibit the utility or performance of the …
Need a python script to copy yesterday files to a destination location and gzip them in destination directory
This question is regarding modifying the below code which is supposed to take f string as the path and within the sn variable looks for all the files that exist there.

Two things I need to know is

1.) what is the correct syntax for me to loop through and view each file within 'sn'…  I know it has to do with for each but unable to come up with the correct syntax to make it work.
2.) ...and what should 'sn' be declared originally besides variant?

This coding below was found doing a search on youtube as the fastest method to get any files within a folder at this link:

Dim sSQL As String
Dim f As String
Dim sn As Variant
f = "C:\Temp\File Sorter\Samples"
sn = Split(CreateObject("wscript.shell").exec("cmd /c Dir """ & f & ".pdf" & """ /s /a /b").stdout.readall, vbCrLf)

Open in new window

I'm really looking for some code to try and paste to see if works.  I'm not a student looking for an answer but have other solutions I'm having to figure out and work on.  Don't have time to try this method but if you have time to solve and know it right away I appreciate the direct answer.   Thank you in advance.
need an archival script that runs on a nighty basis

we maintain our logs for a given month in a directory


on a nightly basis we have to copy logs to corresponding months folder with date and tar and gzip

if the directory does not exist for the month we have to create it when copy and tar the files,

ex : /a/b/c/archieve/`date +`'%Y-%m'`/`

we should be maintaining the archived files for the last 3 days and hould be removed older than 3 days
Office 365 user mailbox and online archive full issue: I am the new IT manager for a company and have inherited a problem. The company has never had policies for mailbox size enforcement or archiving. I have a user with a full mailbox (49.5GB), a 180GB In Place/online archive, and ten local pst files from previous reduction attempts.

I'd like to move all older mail in to a different online archive, and free up the current mailbox and OA to function properly, be modest in size, and also establish better mail management techniques (I'll train the user in the last bit).

Plan A: I'm looking for Azure Exchange powershell commands that will move mail from the active online archive to a  new, secondary user online archive I created for him. My hope is to move all mail older than a year over to the secondary archive, which will be smaller in size than the current one, and then import the local psts in to it so that all the old emails are in one location.

Plan B: Perform the requisite tasks to switch them to the new primary and online mailboxes, leaving the original as the long term holding archive (less desirable, as I won't be able to bring all the old psts in to the almost full one).

I'm looking for powershell commands to copy from one OA to another, and optionally remove a certain date period from both main and OA.

Please ask if I haven't been clear enough about the situation or what I'm asking for. Thanks in advance!
Using the last SharePoint site in Office 365, and after some changes (see below), all files in SharePoint Sync as marked Read Only (Padlock icon)
All files are Read OnlyIndeed if you try to save the file after making a change, you are warned that this file is Read Only and to save using a new name or in a different location.

When editing a file in Word, all files have the following error message: "REQUIRED PROPERTIES To save to the server, correct the invalid or missing required properties"
Required PropertiesWhen you click on the "Edit Properties" button, Word is asking to add a Title and a File Type. If you fail to enter a File Type, you cannot proceed.
PropertiesSame "error" appears when checking the file online
Properties while online
ALL the files and folder in that Site are affected
There are 54K+ files & ~6,000 folders affected

1. Files were previously uploaded without issues by dropping them in a SharePoint synced folder on a Windows 10 PC using OneDrive one demand
2. There was no issue initially with Read Only or Properties missing
3. We had to restart the above process because it had been stopped in the middle
4. It is only after this second upload that their Read Only & Properties settings were changed.

Google has not helped so far.
* Only one possible root cause (but no fix): Changing the Default View on the SharePoint Site web page that lists all the Documents in that Site has been shown to create …

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.