Hi,
I'm new to unix shell scripting so I need some help here. I've been asked to create a script for work that will synchronize the user ids and group ids in the /etc/passwd and /etc/group files with other machines. I know that this is playing with fire a little but it is still required. When I run the following script without sudo then it doesn't execute the -add and -mod commands, but when I run it with sudo (i.e. sudo ./script) then I get the following errors:
[: 30: ==: unexpected operator
./script: 30: groupnum++: not found
for the first loop and
[: 63: ==: unexpected operator
./script: 63: usernum++: not found
for the second loop.
I would NOT suggest running the following script (though I don't think it can do any harm if you use your current passwd and group files as input and input2), but if anyone could point out any errors or offer any ideas it would be greatly appreciated. input and input2 are hypothetically the passwd and group files copied from another machine:
The ((usernum++)) syntax is not supported by all shells, so add a shebang line (e.g. #!/usr/bin/bash) to the beginning of the script to ensure it runs under the required shell when using sudo.
Especially because you say yourself that you are "new to unix shell scripting" i will tell you the following - because you are new you have no habits which you would have to change and developing good habits is not more work than developing bad ones:
Do not use backticks, like in "var=`some_command`". Backticks are only included in modern shells for compatibility reasons. They are hard to read and easily overseen or misread. Furthermore backticks cannot be nested.
The modern replacement is "$(...)", which you can treat like a variable. It will - quite like the backtick construction - open a subshell, execute the commands and close the subshell again. The output will be in the "variables content".
Example:
This construct could also be nested and you can even use it in further constructs without having to assign it to a variable:
Another thing is: you can use the shells string manipulating capabilities instead of relying on tools like awk or sed. Reading a file formatted like /etc/passwd (line=records, field separator=":") could be done the following way: we set the special shell variable "IFS" (the Internal Field Separator) to ":" for this operation (per default it is a space char) and then read several variables in one pass:
As you see you can even skip several fields this way - simply use a variable you do not use.
Save for that your script is very well written - even more so taking into account that you just started. Keep up the good work.
I'm new to utilities like socat and netcat and I'm not clear if they will do what I need.
I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes.
I'm using Cygwin bash on Windows 10.
My... (3 Replies)
Hello all,
I am facing a weird issue while executing a code below -
#!/bin/bash
cd /wload/baot/home/baotasa0/sandboxes_finance/ext_ukba_bde/pset
sh UKBA_publish.sh UKBA 28082015 3
if
then
echo "Param file conversion for all the areas are completed, please check in your home directory"... (2 Replies)
I am trying to call a script(callingscript.sh) from a master script(masterscript.sh) to get string type value from calling script to master script. I have used scripts mentioned below.
#masterscript.sh
./callingscript.sh
echo $fileExist
#callingscript.sh
echo "The script is called"... (2 Replies)
I am using blow script :--
#!/bin/bash
FIND=$(ps -elf | grep "snmp_trap.sh" | grep -v grep) #check snmp_trap.sh is running or not
if
then
# echo "process found"
exit 0;
else
echo "process not found"
exec /home/Ketan_r /snmp_trap.sh 2>&1 & disown -h ... (1 Reply)
Hi guys
I have a shell script that executes sql statemets and sends the output to a file.the script takes in parameters executes sql and sends the result to an output file.
#!/bin/sh
echo " $2 $3 $4 $5 $6 $7
isql -w400 -U$2 -S$5 -P$3 << xxx
use $4
go
print"**Changes to the table... (0 Replies)