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

x

Shell Scripting

10K

Solutions

6K

Contributors

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

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

Background
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 …
0
Become a CompTIA Certified Healthcare IT Tech
LVL 12
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Hi Experts,

I am looking for custom weblogic admin server startup script which can redirect output "directory_path/AdminServer.out" file. and rotate each time we execute the startup script so that older .out file moves to backup file.

example :

AdminServer.out
AdminServer.out0001
AdminServer.out0002
AdminServer.out0003
.....
AdminServer.out000X


Any help will be really appreciated.

Thank you
Neha
0
Experts,

In a file I need to have the possibility to grep a pattern in the first or in the second column.  If found the entire line is returned.
Comes with it that that pattern is a variable.

Grepping the var in the first column is easy, but I can't get it to work for the second column (sep is >).

var=MSPROXML


MSUPDSEOT>MSPROXMLH
VSRFUMVTT>MSPROXMLH
FT0Y6600_T>MSPROXMLH
MSINSMSGT>MSPROXMLH
MSPROXMLT>XGGENEXTH
MSPROXMLT>CD0E0100_H
MSPROXMLT>FT0Y3600_H
MSPROXMLT>PC0R3420_H
MSPROXMLT>WO0A0020_H

This is what would need to be returned:

MSUPDSEOT>MSPROXMLH
VSRFUMVTT>MSPROXMLH
FT0Y6600_T>MSPROXMLH
MSINSMSGT>MSPROXMLH

Thank you for  your help.
Watnog
0
Let's start with a useless example of input redirection:

less 1< /test.txt

Open in new window


The result is:

Missing filename ("less --help" for help)

This I understand, because:

LESS-PROCESS:
FD 0 <- terminal file (keyboard)
FD 1 <- /test.txt
FD 2 -> terminal file (monitor)

FD 0 needs to get some content from a file, but there is no file in this case. There is /test.txt but it points to the wrong fd. Now let's take a look at a useless example of output redirection:

less 0> /test.txt

Open in new window


LESS-PROCESS:
FD 0 -> /test.txt
FD 1 -> terminal file (monitor)
FD 2 -> terminal file (monitor)

The program doesn't give file descriptor 0 some output, so "nothing" will be written to /test.txt. That why you will always end up with an empty /test.txt file. File descriptor 0 opens /test.txt for writing and not for reading. So the less-process doesn't get any file to read from. Then why the result is not:

Missing filename ("less --help" for help)

Instead, less is acting as it got an empty file as input. The file /test.txt is empty in the end, but this is about output redirection and not about input redirection, so there is no input. That's the reason why I would expect "Missing filename". Why this is not the case?
0
See: https://stackoverflow.com/questions/6170598/can-anyone-explain-to-me-what-the-purpose-of-dev-tty


You can start with the POSIX spec. From there, read about the "controlling terminal" of a process.

But just for example... /dev/tty is how a command like "ssh" can read your password even if its standard input comes from somewhere else:

tar cf - . | ssh dest 'tar xf -'

Open in new window


If ssh decides to prompt you for a password, it will read it from /dev/tty instead of stdin.

Conceptually, /dev/tty is "the keyboard and text terminal". More or less.

Let's say my "terminal-file" of the current session is /dev/pts/1. In such a case, then what's the difference between "/dev/pts/1" and "/dev/tty"? And if they are basically the same, then why  "/dev/tty" is used instead of "/dev/pts/1"?

And:

/dev/tty is how a command like "ssh" can read your password even if its standard input comes from somewhere else

Let's say the standard input comes from somewhere else, so let's say we have:

FD 0 <- file
FD 1 -> /dev/pts/1
FD 2 -> /dev/pts/1

How I see it: the fact that the standard input comes from somewhere else doesn't mean that /dev/pts/1 can not be read? The password comes from the keyboard and /dev/pts/1 represents i.a. the keyboard, right? So I still don't see what exactly the purpose is of /dev/tty?

@noci: I know you know the answer, but I don't understand your explanation so I've made this post so maybe other people can explain it to me in a way that I understand it.
0
On a Redhat Linux system running a bash shell script I need some help with an if then statement that has more than 2 conditions. I basically want to check for this
A AND B or C  
A AND B or D
A AND B or E

Something along these lines but it doesn't work and wondered if I have the correct usage of brackets. It's not what's contained for evaluation that's the issue it's the syntax of the AND and OR where there's more than two conditions that I am struggling with.

if [[ $(find /opt/app -name httptd*.conf | grep -v grep | grep -c http) -eq 0 ] && [ ! -f /etc/init.d/apache ] ||  [ $(find /app -name http*.conf | grep -v grep | grep -c http) -eq 0 ]] || \
[[ $(find /opt/app -name httptd*.conf | grep -v grep | grep -c http) -eq 0 ] && [ ! -f /etc/init.d/apache ] ||  [ $(find /application -name http*.conf | grep -v grep | grep -c http) -eq 0 ]] || \
[[ $(find /opt/app -name httptd*.conf | grep -v grep | grep -c http) -eq 0 ] && [ ! -f /etc/init.d/apache ] ||  [ $(find /application -name manifest* | grep -v grep | grep -c http) -eq 0 ]] ; then
.....
0
i need to truncate and load data into informix table .. is there a way by which i can used dbload to do it i am using a shell script to load the data .. but it always appends into the table...also i am trying to do a upshift of character data while loading itself .. but that throws error ... as syntax mismatch while using the utility.. my control file is as below



"control.file" 3 lines, 83 characters
FILE "file.unl" DELIMITER '|' 3;
INSERT INTO TAB1(name,age,salary)
VALUES (UPPER(f01),f02,f03);
0
I'm reading about "redirection of input" on the internet. I understand what's behind it. For example:

command < file.ext

Open in new window


This is equivalent to:

command 0< file.ext

Open in new window


In general, if you have:

command n< file.ext

Open in new window


then the contents of file.ext go to file descriptor "n" as input. I've checked different websites explaining "input redirection". However, the problem is that I didn't see any good example. I'll discuss some examples I saw:

cat < file.txt

Open in new window


Then I'm thinking, "cat file.txt" does the same, so why do we need it? Another example:

sort < file_list.txt > sorted_file_list.txt

Open in new window


Then I'm thinking, "sort file_list.txt > sorted_file_list.txt" does the same, so why do we need it? Another example:

more < /etc/passwd

Open in new window


Then I'm thinking, "more /etc/passwd" does the same, so why do we need it? That's why these are not really good examples in my opinion. What is a good example to explain the purpose of input redirection in a terminal-window?

Probably internally something like "cat file.txt" is being treated as "cat 0< file.txt" (input redirection), but in a terminal-window ... when it really does make sense to use an "input redirection" in a terminal-window? Does someone have a good example?
0
Why does for loop return only the first entry from my lists?  Stripped down script as follows.

groups=(baremetal production staging)
baremetal=(test1 test2)
production=(test3)
staging=()

for group in ${groups[@]}
  do
        echo $group

    for host in ${!group}   # only adds the 1st
      do
           echo $host

      done
  done


./test.sh
baremetal
test1
production
test3
staging
0
First I create a regular file with some contents (manual page of find command):

man find > test.txt

Open in new window


Then I use the less command to display some of these contents:

less test.txt

Open in new window


Now I press CTRL-Z to suspend the process. The process is still open, so now I can execute this command:

lsof | grep 'less'

Open in new window


By doing this, I get an idea which files are open with respect to the less-process. My result:

COMMAND  PID    USER  FD   TYPE  DEVICE  SIZE/OFF  NODE       NAME
less     24565  root  cwd  DIR   0,38    4096      21473055   /
less     24565  root  rtd  DIR   0,38    4096      21473055   /
less     24565  root  txt  REG   0,38    149944    22143102   /usr/bin/less
less     24565  root  mem  REG   9,1               22143102   /usr/bin/less (path dev=0,38)
less     24565  root  mem  REG   9,1               22135172   /usr/lib/locale/locale-archive-rpm (path dev=0,38)
less     24565  root  mem  REG   9,1               21741879   /lib64/libc-2.12.so (path dev=0,38)
less     24565  root  mem  REG   9,1               22265955   /usr/local/lib/libpcre.so.0.0.1 (path dev=0,38)
less     24565  root  mem  REG   9,1               21741743   /lib64/libtinfo.so.5.7 (path dev=0,38)
less     24565  root  mem  REG   9,1               21741946   /lib64/ld-2.12.so (path dev=0,38)
less     24565  root  0u   CHR   136,1   0t0       4          /dev/pts/1
less     24565  root  1u   CHR   136,1   0t0       4          /dev/pts/1
less     

Open in new window

0
Angular Fundamentals
LVL 12
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Let's say I type the following "in a terminal":

echo 'bla'

Open in new window


In my case, the shell is bash, so I assume the shell/bash-process receives "echo 'bla'" as standard input? Then it sees "echo", so a child process will be started. So then we will have at least:

ECHO PROCESS:
fd 0 (standard input)   <- terminal-file (keyboard)
fd 1 (standard output)  -> terminal-file (monitor)
fd 2 (standard error)   -> terminal-file (monitor)

Open in new window


I thought that for this process, only "bla" is the standard input. And then the output is also "bla", so I'll see "bla" on my monitor.

I was just a bit playing with "input redirections" and I noticed that the following does not work:

echo < bla-file.txt

Open in new window


After some Google searches, I found out that "echo" does not read from stdin. However, it prints all of its arguments. So it's working differently than normal. So how I have to see/change this:

ECHO PROCESS:
fd 0 (standard input)   <- terminal-file (keyboard)
fd 1 (standard output)  -> terminal-file (monitor)
fd 2 (standard error)   -> terminal-file (monitor)

Open in new window


I thought every process by default has fd's 0,1,2? But if fd 0 would be there something like this:

fd 0 (standard input)   <- nothing

Open in new window


Then it should be still possible to redirect (input) to something. So this means I can not see it like that. Does this mean that the echo process doesn't have a fd 0 at all? Or I must not see "echo" as a process with a fd table et cetera?

But the echo command displays something on my monitor, so at least this should be there:

fd 1 (standard output)  -> terminal-file (monitor)

Open in new window

1
I have a list of file names that i loop through and move to a destination directory.   Now i need to look for files that have *_NTL*  in the file name and then do something.  

I have this.

if [ ${archive_dir}/$name 2>/dev/null ]
      then
       cp ${archive_dir}/$name ${data_dir}/NTL_${filename}_${DateTime}.csv
      else
      cp ${archive_dir}/$name ${dest_dir}$_{filename}_${DateTime}.csv
fi

any assistance what i am doing wrong, would be great.

Thanks,
bje
0
Redirecting output ([n]>[|]word), when does "n" greater than 2 make sense?

This question is about redirecting output in bash, see: https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Redirecting-Output

The general format for redirecting output is:

[n]>[|]word

I already understand the basics (you're just redirecting a file descriptor to a file for writing). But, can someone give me an example when it does make sense to use for example: 3>. In other words, if n is greater than stdin, stdout, stderr (>2), for what would you need it?

I can do:

echo 'test' 3> test-file.txt

Open in new window


This will not write anything to "test-file.txt". This is logical, because now there is just a file descriptor with number 3 pointing to test-file.txt for writing, but there is no input to fd=3 so there is also nothing to write.

The only way to give it some input is to connect file descriptor 3 for reading with a file (or connect it to the output of a pipe). But if you would do that, then fd 3 doesn't point to test-file.txt anymore. So then in the end, fd 3 was connected to test-file.txt without any reason.

So in what kind of situation it's useful to use >n with n greater than 2?
0
Experts,

I am looking for a script that can take a list of hostnames ($mypcs) and provide the IP, MacAddress, and Hostname of each of the computers in question.

I would like this script to also be able to tell when computers were not reachable..

I have been working on the following but haven't gotten it to work property yet and need this information ASAP.


Function Get-Mac { 
    $ComputerName = $mypcs 
    $ErrorActionPreference = 'Stop'
    
    $COlResults = foreach ($Computer in $ComputerName) {     
        invoke-command -ComputerName $computer -Credential $cred -ScriptBlock {
            Try{
                $WMI = gwmi -class "Win32_NetworkAdapterConfiguration" |? IpEnabled -EQ "True" | select PSComputerName, description, ipAddress, MACAddress | FT -AutoSize;
                $WMI        
            }catch {
                    Write-Warning "System is not reachable : $Computer";Continue
             }
            
        
    }

    }
    $COlResults
}
. Get-Mac

Open in new window

0
Hello,

I have a bash script that I want to output the variables into a .csv file.  Most of the variable I can have the variable output on one line but I'm having a problme with variables that are multiline. The two variables are passed the process id of a process and the process listing contains new line characters. I thought if I stripped out the newline characters that you could assign it to the variable but it complains "No such file or directory in the variable substition".  The other variable is just netstat -an | grep <port>.  I don't know how to use arrays in bash but ideally i'd like to be able to keep the newlines in the csv file, if it's possible, since i'm sticking with comma as the separator. Any help would be really appreciated. Hitting my head off the wall!

VAR1="$(ps -ef | grep 27656 | tr '\n' ' ' | sed -e '/s   */g')"
(printf '%s\n' $VAR1)
0
File descriptor table:      Open file table
FD 0 (stdin)                ?
FD 1 (stdout)               ?
FD 2 (stderr)               ?

Open in new window


By default file descriptors 0, 1 and 2 are associated with the terminal. The keyboard input is associated with the standard input. The monitor is associated with the standard output and standard error.

The question is: Do fd 0,1,2 all refer to the same entry in the "open file table"? Or do they refer to two entries?

 
FD 0  -> entry A
FD 1    
       > entry B  
FD 2    

Open in new window


Or do they refer to three entries?

 
FD 0  -> entry A
FD 1  -> entry B
FD 2  -> entry C

Open in new window


This seems a pretty basic question, but I'm reading different things about it on the internet.

See: https://www.usna.edu/Users/cs/aviv/classes/ic221/s16/lec/21/lec.html#orgheadline6

If fd 1 and fd 2 refer to a different entry in the open file table, then this should be also the case for fd 0. So according to this website, they refer to three different entries in the open file table.
 
But now see: https://www.enseignement.polytechnique.fr/informatique/INF422/INF422_8.pdf#page=160    (page 160, example of no redirection)

There, it's like:

 
FD 0  -> entry A
FD 1    
       > entry B  
FD 2    

Open in new window


So according to that website, they refer to two different entries in the open file table.

And see: https://www.experts-exchange.com/questions/29119936/How-the-open-file-table-entries-look-like-for-stdin-stdout-stderr.html#a42694025


the fd[0] , fd[1] & fd[2] should all point to the same central entry

According to this, they all refer to the same entry in the open file table.

I can execute the following command:

lsof | grep 'bash'

Open in new window


This i.a. prints:


Open in new window

0
Hi All,

I have about 20 Centos OS servers and I need a script that read a text file that contains a list of server names, then go into each server and find out if a particular file exists, if it does, write the host name to fileexsit.txt and it the file doesn't exist, write to a file named filenotexist.txt.

Please help!!


Thanks!
0
By default, the first three rows of a "file descriptor table" consists of:

FD 0 (standard input,  associated with keyboard)
FD 1 (standard output, associated with screen)
FD 2 (standard error,  associated with screen)

Open in new window


These file descriptors point to one or more rows in the "open file table". Imagine we only have these three file descriptors. Then how does the "open file table" look like?

Usually all three file descriptors point to the same file, but that doesn't mean they point to the same entry in the "open file table". So how the open file table looks like?

_ | offset | reference count | permissions | flags | pointers
_ |    ?   |        ?        |      ?      |   ?
possible more rows

Open in new window


The lsof command shows for example:

lsof        721    root    0u      CHR              136,1       0t0          4 /dev/pts/1
lsof        721    root    1u      CHR              136,1       0t0          4 /dev/pts/1
lsof        721    root    2u      CHR              136,1       0t0          4 /dev/pts/1

Open in new window


The file "/dev/pts/1" is CHR (character special file). They all point to the same file.
I'm also wondering why it's for example "0u" and not "0r"? The file descriptor 0 stands for input, so it only has to read something.

r for read access;
w for write access;
u for read and write access;

Open in new window


I would expect something like: 0r, 1w, 2w instead of 0u, 1u, 2u? And what are the offsets et cetera?
0
[root@myserver~]# /bin/ksh
# echo $SHELL
/bin/bash

I wonder why ksh is not showing up when i did ksh or /bin/ksh?

I am used with bash, specific app uses ksh so i had to switch.
0
Exploring SQL Server 2016: Fundamentals
LVL 12
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

My question is about a pipe/pipeline in Unix. And it's especially about the FD (File descriptor) numbers/integers associated with it.
This is what I would expect:

  PROCESS A           PROCESS B
  0 stdin
  1 stdout -> pipe -> 0 stdin
  2 stderr            1 stdout
                      2 stderr

Open in new window


However, on the internet I'm reading different things.

For example, see: http://pubs.opengroup.org/onlinepubs/009696799/functions/pipe.html


Their integer values shall be the two lowest available at the time of the pipe() call.

Also see the images here: http://www.rozmichelle.com/pipes-forks-dups/#attachment_7362

Why new file descriptors are created? Why they don't use stdout (1) of the parent process? They could just redirect this existing
file descriptor (1/stdout) to the pipe. Do I miss something?
1
Hi,

Im trying to develop a script that will strip the msexchangeguid attribute from a particular OU only, once a week as a scheduled task.
Im almost there.

The first part of the script successfully identifies all users in that OU that has a non-NULL msExchangeGUID
 
Get-ADUser -SearchBase "OU=Disabled-Objects,dc=my,dc=domain,dc=com" -filter * -Properties * | ? {$_.msExchMailboxGuid -ne $null}

Open in new window


The next part of the linked script will just give me the SAMAccountNames of those users with a non-Null msExchangeGUID from the above command
Select-Object SamAccountName

Open in new window


Put the final part of setting the attribute to be Null (0000) doesnt take the SamAccountNames for action
Set-RemoteMailbox  -ExchangeGUID 00000000-0000-0000-0000-00000000000

Open in new window


Here it is in one non-functioning line.

Get-ADUser -SearchBase "OU=Disabled-Objects,dc=my,dc=domain,dc=com" -filter * -Properties * | ? {$_.msExchMailboxGuid -ne $null} | Select-Object SamAccountName | Set-RemoteMailbox  -ExchangeGUID 00000000-0000-0000-0000-000000000000

Open in new window



I also thought about i could put the output of the first two lines into a text file, that I think use as a variable
Get-ADUser -SearchBase "OU=Disabled-Objects,dc=my,dc=domain,dc=com" -filter * -Properties * | ? {$_.msExchMailboxGuid -ne $null} | Select-Object SamAccountName > users.txt

Open in new window


$users=Get-content users.txt
Foreach($user in $users){
get-aduser $user| set-aduser -clear msExchMailboxGuid
}

Open in new window


But the users.txt has trailing whitespaces after the username so the  
get-aduser $user| set-aduser -clear msExchMailboxGuid

Open in new window

looks like
get-aduser "USERNAME        "| set-aduser -clear msExchMailboxGuid

Open in new window

when it runs. It fails on the excess whitespaces.

Any help from Powershell gurus out there?
Thanks!
0
I am, at best, a novice user when it comes to writing Powershell scripts, so any simplified explanation to this question would be appreciated.
I am accessing one of our vendors API services. In order to access it, I need to generate an authentication token. I have the url to send the request to, and I have the username and password. What I am not sure of, is how to write the script to retrieve the token. Is there a "generic" method to do this?

Again, feel free to break it down to the most basic of terms. I won't be offended. :)

Thank you in advance.
0
I'm trying to parse out the hostname of the machine from /etc/hosts from /etc/hosts in order to find out the IP of the server and only use host to lookup the IP if it's not in /etc/hosts. There are multiple NICS on the server.  The issue is in the /etc/hosts it looks like this

10.12.4.15 gonzo gonzo.domainxxx
10.5.6.19 gonzo-bk gonzo-bk.domainxxx
10.10.3.19 gonzo-sn gonzo-sn.domainxxx
?
I've used grep -w to try and grep out the hostname but it still returns all the IP's in /etc/hosts. Is there a better way to parse this?

#!/bin/bash

# Get the server's IP address
if [ $( cat /etc/hosts | grep -cw $(hostname)) -ne 0 ];then
            # (1)Read the IP from the hosts file
           MGTIP=$(cat /etc/hosts | grep -w "$(hostname)" | awk '{print $1}')
else
       # (2)Get the IP with nslookup if it's not in /etc/hosts
       MGMTIP=$(host "$(hostname)" |  awk  '{print $5}')
fi
0
I'd like to determine in my bash script which NIC the default gateway is on in order to determine if i need to add static routes via the script or not. Since there are two instances of 0.0.0.0 in the routing table where the default gateway is I'n trying to determine the NIC mentioned in the routing table where there are two instances of 0.0.0.0. Then if it matches ${DftNic} below I no I won't have to add static routes on the server. Scratching my head on this so any help would terrific.

DftNic=$(ip route show | grep default  awk '{print $7}')
0
Batch file to ping all network hosts to determinate witch are powered up and store output to a text file

Hello, I need to write a Batch file to ping all network hosts (readed from a hostslist.txt) and write UP/DOWN state to a text file.
0

Shell Scripting

10K

Solutions

6K

Contributors

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.