Sample Header Ad - 728x90

Unix & Linux Stack Exchange

Q&A for users of Linux, FreeBSD and other Unix-like operating systems

Latest Questions

0 votes
4 answers
1868 views
create variables from CSV with varying number of fields
Looking for some help turning a CSV into variables. I tried using IFS, but seems you need to define the number of fields. I need something that can handle varying number of fields. *I am modifying my original question with the current code I'm using (taken from the answer provided by hschou) which i...
Looking for some help turning a CSV into variables. I tried using IFS, but seems you need to define the number of fields. I need something that can handle varying number of fields. *I am modifying my original question with the current code I'm using (taken from the answer provided by hschou) which includes updated variable names using type instead of row, section etc. I'm sure you can tell by my code, but I am pretty green with scripting, so I am looking for help to determine if and how I should add another loop or take a different approach to parsing the typeC data because although they follow the same format, there is only one entry for each of the typeA and typeB data, and there can be between 1-15 entries for the typeC data. The goal being only 3 files, one for each of the data types. Data format: Container: PL[1-100] TypeA: [1-20].[1-100].[1-1000].[1-100]-[1-100] TypeB: [1-20].[1-100].[1-1000].[1-100]-[1-100] TypeC (1 to 15 entries): [1-20].[1-100].[1-1000].[1-100]-[1-100] *There is no header in the CSV, but if there were it would look like this (Container, typeA, and typeB data always being in position 1,2,3, and typeC data being all that follow): Container,typeA,typeB,typeC,tycpeC,typeC,typeC,typeC,.. CSV: PL3,12.1.4.5-77,13.6.4.5-20,17.3.577.9-29,17.3.779.12-33,17.3.802.12-60,17.3.917.12-45,17.3.956.12-63,17.3.993.12-42 PL4,12.1.4.5-78,13.6.4.5-21,17.3.577.9-30,17.3.779.12-34 PL5,12.1.4.5-79,13.6.4.5-22,17.3.577.9-31,17.3.779.12-35,17.3.802.12-62,17.3.917.12-47 PL6,12.1.4.5-80,13.6.4.5-23,17.3.577.9-32,17.3.779.12-36,17.3.802.12-63,17.3.917.12-48,17.3.956.12-66 PL7,12.1.4.5-81,13.6.4.5-24,17.3.577.9-33,17.3.779.12-37,17.3.802.12-64,17.3.917.12-49,17.3.956.12-67,17.3.993.12-46 PL8,12.1.4.5-82,13.6.4.5-25,17.3.577.9-34 Code: #!/bin/bash #Set input file _input="input.csv" # Pull variables in from csv # read file using while loop while read; do declare -a COL=( ${REPLY//,/ } ) echo -e "containerID=${COL}\ntypeA=${COL}\ntypeB=${COL}" >/tmp/typelist.txt idx=1 while [ $idx -lt 10 ]; do echo "typeC$idx=${COL[$((idx+2))]}" >>/tmp/typelist.txt let idx=idx+1 #whack off empty variables sed '/\=$/d' /tmp/typelist.txt > /tmp/typelist2.txt && mv /tmp/typelist2.txt /tmp/typelist.txt #set variables from temp file . /tmp/typelist.txt done sleep 1 #Parse data in this loop.# echo -e "\n" echo "Begin Processing for $container" #echo $typeA #echo $typeB #echo $typeC #echo -e "\n" #Strip - from sub data for extra parsing typeAsub="$(echo "$typeA" | sed 's/\-.*$//')" typeBsub="$(echo "$typeB" | sed 's/\-.*$//')" typeCsub1="$(echo "$typeC1" | sed 's/\-.*$//')" #strip out first two decimils for extra parsing typeAprefix="$(echo "$typeA" | cut -d "." -f1-2)" typeBprefix="$(echo "$typeB" | cut -d "." -f1-2)" typeCprefix1="$(echo "$typeC1" | cut -d "." -f1-2)" #echo $typeAsub #echo $typeBsub #echo $typeCsub1 #echo -e "\n" #echo $typeAprefix #echo $typeBprefix #echo $typeCprefix1 #echo -e "\n" echo "Getting typeA dataset for $typeA" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeAsub" -g all > "$container"typeA-dataset" sleep 1 echo "Getting typeB dataset for $typeB" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeBsub" -g all > "$container"typeB-dataset" sleep 1 echo "Getting typeC dataset for $typeC1" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeCsub" -g all > "$container"typeC-dataset" sleep 1 echo "Getting additional typeC datasets for $typeC2-15" #call api script to pull data ; echo out for test echo "API-gather -option -b "$typeCsub2-15" -g all >> "$container"typeC-dataset" sleep 1 echo -e "\n" done < "$_input" exit 0 Speed isnt a concern, but if I've done anything really stupid up there, feel free to slap me in the right direction. :)
Jdubyas (45 rep)
Jul 12, 2017, 05:09 AM • Last activity: Aug 6, 2025, 12:04 AM
36 votes
12 answers
56155 views
How to compare a program's version in a shell script?
Suppose I want to compare `gcc` version to see whether the system has the minimum version installed or not. To check the `gcc` version, I executed the following gcc --version | head -n1 | cut -d" " -f4 The output was 4.8.5 So, I wrote a simple `if` statement to check this version against some other...
Suppose I want to compare gcc version to see whether the system has the minimum version installed or not. To check the gcc version, I executed the following gcc --version | head -n1 | cut -d" " -f4 The output was 4.8.5 So, I wrote a simple if statement to check this version against some other value if [ "$(gcc --version | head -n1 | cut -d" " -f4)" -lt 5.0.0 ]; then echo "Less than 5.0.0" else echo "Greater than 5.0.0" fi But it throws an error: [: integer expression expected: 4.8.5 I understood my mistake that I was using strings to compare and the -lt requires integer. So, is there any other way to compare the versions?
Abhimanyu Saharan (911 rep)
May 27, 2016, 02:01 PM • Last activity: Aug 5, 2025, 10:58 AM
32 votes
8 answers
14361 views
Simultaneously calculate multiple digests (md5, sha256)?
Under the assumption that disk I/O and free RAM is a bottleneck (while CPU time is not the limitation), does a tool exist that can calculate multiple message digests at once? I am particularly interested in calculating the MD-5 and SHA-256 digests of large files (size in gigabytes), preferably in pa...
Under the assumption that disk I/O and free RAM is a bottleneck (while CPU time is not the limitation), does a tool exist that can calculate multiple message digests at once? I am particularly interested in calculating the MD-5 and SHA-256 digests of large files (size in gigabytes), preferably in parallel. I have tried openssl dgst -sha256 -md5, but it only calculates the hash using one algorithm. Pseudo-code for the expected behavior: for each block: for each algorithm: hash_state[algorithm].update(block) for each algorithm: print algorithm, hash_state[algorithm].final_hash()
Lekensteyn (21600 rep)
Oct 23, 2014, 10:00 AM • Last activity: Aug 4, 2025, 06:51 AM
23 votes
2 answers
9078 views
List of shells that support `local` keyword for defining local variables
I know that Bash and Zsh support `local` variables, but there are systems only have POSIX-compatible shells. And `local` is undefined in POSIX shells. So I want to ask which shells support `local` keyword for defining local variables? **Edit**: About shells I mean the default `/bin/sh` shell.
I know that Bash and Zsh support local variables, but there are systems only have POSIX-compatible shells. And local is undefined in POSIX shells. So I want to ask which shells support local keyword for defining local variables? **Edit**: About shells I mean the default /bin/sh shell.
mja (1525 rep)
Jan 10, 2019, 02:13 PM • Last activity: Aug 1, 2025, 01:19 PM
1 votes
1 answers
3177 views
How to automatically detect and write to usb with variable spaces in its name
I am doing the second BASH exercise from [TLDP Bash-Scripting Guide][1], and I have most of it figured out up until the part when it comes time to copy the compressed files to an inserted USB. > Home Directory Listing > > Perform a recursive directory listing on the user's home directory and save th...
I am doing the second BASH exercise from TLDP Bash-Scripting Guide , and I have most of it figured out up until the part when it comes time to copy the compressed files to an inserted USB. > Home Directory Listing > > Perform a recursive directory listing on the user's home directory and save the information to a file. Compress the file, have the script > prompt the user to insert a USB flash drive, then press ENTER. > Finally, save the file to the flash drive after making certain the > flash drive has properly mounted by parsing the output of df. Note > that the flash drive must be unmounted before it is removed. As I progress with the script it is becoming less ..elegant, and was wondering if there was a better way to do this. I know creating files is likely not the most efficient way to do the comparisons, but have not got the shell expansions figured yet, and intend to change those once I get it working. The problem specifically is, to ensure that the usb is mounted and that I am writing to the USB and nowhere else. I am comparing the last line of df after the USB is plugged in with the last line of df from the diff between df before USB is plugged in and df after USB is plugged in, and looping until they match. Unfortunately, the diff result starts with a >, but I intend to use sed to get rid of that. The real problem is the path to where my usb is mounted is: > /media/flerb/"Title of USB with spaces" To make this portable for USBs that may have different names is my best bet from here to do something with awk and field separators? And as a follow-up, I know this is pretty inelegant, and wonder if there is a cleaner way to go about this...especially because this is the second exercise and still in EASY. The output from the df tails is: /dev/sdb1 15611904 8120352 7491552 53% /media/flerb/CENTOS 7 X8 > /dev/sdb1 15611904 8120352 7491552 53% /media/flerb/CENTOS 7 X8 The script so far 1 #!/bin/bash 2 3 if [ "$UID" -eq 0 ] ; then 4 echo "Don't run this as root" 5 exit 1 6 fi 7 8 #Create a backup file with the date as title in a backup directory 9 BACKUP_DIR="$HOME/backup" 10 DATE_OF_COPY=$(date --rfc-3339=date) 11 BACKUP_FILE="$BACKUP_DIR/$DATE_OF_COPY" 12 13 [ -d "$BACKUP_DIR" ] || mkdir -m 700 "$BACKUP_DIR" 14 15 #find all files recursively in $HOME directory 16 find -P $HOME >> "$BACKUP_FILE" 17 18 #use lzma to compress 19 xz -zk --format=auto --check=sha256 --threads=0 "$BACKUP_FILE" 20 21 #making files to use in operations 22 BEFORE="$BACKUP_DIR"/before_usb.txt 23 AFTER="$BACKUP_DIR"/after_usb.txt 24 DIFFERENCE="$BACKUP_DIR"/difference.txt 25 26 df > "$BEFORE" 27 read -p 'Enter USB and press any button' ok 28 sleep 2 29 df > "$AFTER" 30 diff "$BEFORE" "$AFTER" > "$DIFFERENCE" 31 sleep 2 32 echo 33 34 TAIL_AFTER=$(tail -n 1 "$AFTER") 35 TAIL_DIFF=$(tail -n 1 "$DIFFERENCE") 36 37 until [ "$TAIL_AFTER" == "$TAIL_DIFF" ] ; 38 do 39 echo "Not yet" 40 df > "$AFTER" 41 TAIL_AFTER=$(tail -n 1 "$AFTER") 42 diff "$BEFORE" "$AFTER" > "$DIFFERENCE" 43 TAIL_DIFF=$(tail -n 1 "$DIFFERENCE") 44 echo "$TAIL_AFTER" 45 echo "$TAIL_DIFF" 46 sleep 1 47 48 done 49 exit $?
flerb (983 rep)
Jul 12, 2017, 05:28 PM • Last activity: Jul 30, 2025, 05:07 AM
-2 votes
2 answers
50 views
HISTTIMEFORMAT not working as desired in RHEL 8 bash 4.4.20
so trying to capture history with date and readable timestamps AND the command should appear on same line. following is failing: Bash ver is: GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu) OS: RHEL 8.x in .bashrc.... setopt EXTENDED_HISTORY export HISTTIMEFORMAT='%F %I:%M:%S %T' #expo...
so trying to capture history with date and readable timestamps AND the command should appear on same line. following is failing: Bash ver is: GNU bash, version 4.4.20(1)-release (x86_64-redhat-linux-gnu) OS: RHEL 8.x in .bashrc.... setopt EXTENDED_HISTORY export HISTTIMEFORMAT='%F %I:%M:%S %T' #export HISTTIMEFORMAT="%F %T " export HISTSIZE=1000 export HISTFILE=$HOME/.bash_history-$USER export HISTFILESIZE=1000 export PROMPT_COMMAND='history -a' env variables: $ env|grep HIST HISTCONTROL=ignoredups HISTTIMEFORMAT=%F %I:%M:%S %T HISTFILE=/home/user1/.bash_history-user1 HISTSIZE=1000 HISTFILESIZE=1000 the records appear as: #1753745611 cd #1753745616 ls -ltra|tail #1753745626 cat .profile #1753745633 cat .kshrc **expected:** Mon Jul 28 23:33:31 GMT 2025 cd Mon Jul 28 23:33:36 GMT 2025 ls -ltra|tail Mon Jul 28 23:33:46 GMT 2025 cat .profile Mon Jul 28 23:33:53 GMT 2025 cat .kshrc two problems with this. 1. Timestamp appears in UNIX EPOCH format. 2. Timestamp and command appears on separate lines. They should be together. Also behaves the same way when using KSH. How can this be fixed? preferably using HISTTIMEFORMAT Thank you.
Rajeev (256 rep)
Jul 29, 2025, 03:33 PM • Last activity: Jul 29, 2025, 10:01 PM
2 votes
1 answers
133 views
Semicolon in conditional structures after the closing double bracket in a bash/zsh script?
Continuing https://unix.stackexchange.com/questions/48805/semicolon-in-conditional-structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket `]]`? In my tests, running ```zsh #!/bin/zsh -- if [[ "a" == "a" ]] then echo "true" else echo "false...
Continuing https://unix.stackexchange.com/questions/48805/semicolon-in-conditional-structures (which handles single brackets), what's the point of having a semicolon after the closing DOUBLE bracket ]]? In my tests, running
#!/bin/zsh --
if [[ "a" == "a" ]] then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]]; then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]]
then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "a" ]];
then
	echo "true"
else
	echo "false"
fi
yields
true
true
true
true
, and running
#!/bin/zsh --
if [[ "a" == "b" ]] then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]]; then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]]
then
	echo "true"
else
	echo "false"
fi

if [[ "a" == "b" ]];
then
	echo "true"
else
	echo "false"
fi
yields
false
false
false
false
No error is reported. In zsh, what's the difference between the conditionals with a semicolon ; after the closing double bracket and the conditionals without a semicolon after the closing double bracket? The same question goes for bash.
user743115 (1 rep)
Jul 26, 2025, 12:42 AM • Last activity: Jul 26, 2025, 06:12 PM
2 votes
2 answers
2490 views
Shell-/Bash-Script to delete old backup files by name and specific pattern
every our backup files of a database are created. The files are named like this: ```` prod20210528_1200.sql.gz pattern: prod`date +\%Y%m%d_%H%M` ````` The pattern could be adjusted if needed. I would like to have a script that: - keeps all backups for the last x (e.g. 3) days - for backups older tha...
every our backup files of a database are created. The files are named like this:
`
prod20210528_1200.sql.gz 
pattern: proddate +\%Y%m%d_%H%M
`` The pattern could be adjusted if needed. I would like to have a script that: - keeps all backups for the last x (e.g. 3) days - for backups older than x (e.g. 3) days only the backup from time 00:00 shall be kept - for backups older than y (e.g. 14) days only one file per week (monday) shall be kept - for backups older than z days (e.g. 90) only one file per month (1st of each month) shall be kept - the script should rather use the filename instead of the date (created) information of the file, if that it possible - the script should run every day Unfortunately, I have very little knowledge of the shell-/bash-script language. I would do something like this: ````` if (file today - (x + 1)) { if (%H_of_file != 00 AND %M_of_file != 00) { delete file } } if (file today - (y + 1)) { if (file != Monday) { delete file } } if (file today - (z + 1)) { if (%m_of_file != 01) { delete file } } Does this makes any sense for you? Thank you very much! All the best, Phantom
Phantom (143 rep)
May 28, 2021, 05:25 PM • Last activity: Jul 26, 2025, 04:04 AM
0 votes
1 answers
2205 views
/usr/share/bash-completion/bash_completion parse error
I am facing an error when run $ source ~/.bashrc. The error is /usr/share/bash-completion/bash_completion:1512: parse error near `|'. bash_completion file (the first line is 1512) : ``` if ! [[ "$i" =~ ^\~.*|^\/.* ]]; then if [[ "$configfile" =~ ^\/etc\/ssh.* ]]; then i="/etc/ssh/$i" else i="$HOME/....
I am facing an error when run $ source ~/.bashrc. The error is /usr/share/bash-completion/bash_completion:1512: parse error near `|'. bash_completion file (the first line is 1512) :
if ! [[ "$i" =~ ^\~.*|^\/.* ]]; then
            if [[ "$configfile" =~ ^\/etc\/ssh.* ]]; then
                i="/etc/ssh/$i"
            else
                i="$HOME/.ssh/$i"
            fi
        fi
Please help me solve this problem. Thanks!
Sơn Nguyễn Ngọc (9 rep)
Feb 19, 2021, 02:12 AM • Last activity: Jul 26, 2025, 02:05 AM
2 votes
2 answers
2844 views
Self update bash script if there are any updates first then continue on, with Git
I'm trying to add the ability for my [ArchLinux installer script][1] to check if it's update-to-date based on rather it matches (or doesn't match) the version number that's on gitlab. The primary script that runs the installer (and all of the numbered script files) is the `aalis.sh` script, it basic...
I'm trying to add the ability for my ArchLinux installer script to check if it's update-to-date based on rather it matches (or doesn't match) the version number that's on gitlab. The primary script that runs the installer (and all of the numbered script files) is the aalis.sh script, it basically goes and runs the other files together. The version numbering would be something like 1.2.3 (major.minor.patch). Basically, whenever I make any changes to the script, I will change the script's version number of gitlab; and I want the script itself to be able to detect that its version number doesn't the match the one on github (for cases where someone has an outdated version of the script and try to run it); and automatically update itself using git fetch origin master then rerun itself using the updated contents.
Nova Leary (43 rep)
Jan 2, 2022, 01:00 AM • Last activity: Jul 24, 2025, 06:06 PM
1 votes
3 answers
2438 views
Searching /usr/dict/words to find words with certain properties
I would like to write a script to search through /usr/dict/words to find all words that meet some criteria I specify. For example, finding all palindromic words (like "racecar", "madam", etc.) or finding all words where the first and second halves reversed also form a word (like "german" and "manger...
I would like to write a script to search through /usr/dict/words to find all words that meet some criteria I specify. For example, finding all palindromic words (like "racecar", "madam", etc.) or finding all words where the first and second halves reversed also form a word (like "german" and "manger"). The framework of the script would be a simple loop to read each word in the dictionary, and I could change the criteria depending on what I want to look for by substituting an expression or something similar. I figure I would need to involve regular expressions somehow (or otherwise find a way to look at individual characters in each word). I would also need a way to compare the characters in my current word to the other words in the dictionary (such as with my second example above). What would be the best tool(s) to use for this task?
user161121
Mar 14, 2016, 11:31 PM • Last activity: Jul 23, 2025, 10:05 AM
0 votes
3 answers
1964 views
sed? - Insert line after a string with special characters to Neutron service
I am attempting to write a bash script that will insert a string after matching on a string in /usr/lib/systemd/system/neutron-server.service I have been able to do this on other files easily as I was just insert variables into neccessary config files, but this one seems to be giving me trouble. I b...
I am attempting to write a bash script that will insert a string after matching on a string in /usr/lib/systemd/system/neutron-server.service I have been able to do this on other files easily as I was just insert variables into neccessary config files, but this one seems to be giving me trouble. I believe the error is that sed is not ignoring the special characters. In my attempt I have tried using sed of single quotes and double quotes (which I understand are for variables, but thought it might change something. Is there a better way of going about this or some special sed flags or syntax I am missing? sed ‘/--config-file /etc/neutron/plugin.ini/a\--config-file /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini‘ /usr/lib/systemd/system/neutron-server TL;DR - Insert --config-file /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini After --config-file /etc/neutron/plugin.ini Orginial File [Unit] Description=OpenStack Neutron Server After=syslog.target network.target [Service] Type=notify User=neutron ExecStart=/usr/bin/neutron-server --config-file /usr/share/neutron/neutron- dist.conf --config-dir /usr/share/neutron/server --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugin.ini --config-dir /etc/neutron/conf.d/common --config-dir /etc/neutron/conf.d/neutron-server - -log-file /var/log/neutron/server.log PrivateTmp=true NotifyAccess=all KillMode=process TimeoutStartSec="infinity" [Install] WantedBy=multi-user.target File after desired change command. [Unit] Description=OpenStack Neutron Server After=syslog.target network.target [Service] Type=notify User=neutron ExecStart=/usr/bin/neutron-server --config-file /usr/share/neutron/neutron- dist.conf --config-dir /usr/share/neutron/server --config-file /etc/neutron/neutron.conf --config-file /etc/neutron/plugin.ini --config- file /etc/neutron/plugins/ml2/ml2_conf_cisco_apic.ini --config-dir /etc/neutron/conf.d/common --config-dir /etc/neutron/conf.d/neutron-server - -log-file /var/log/neutron/server.log PrivateTmp=true NotifyAccess=all KillMode=process TimeoutStartSec="infinity" [Install] WantedBy=multi-user.target
fly2809 (1 rep)
Jun 13, 2017, 08:48 PM • Last activity: Jul 22, 2025, 09:04 AM
0 votes
2 answers
1982 views
How to pass command line arguments to bash script when executing with at?
I have a bash script needs run at a specific time and found out `at` is pretty much does what I need to do. But the problem is I'm not sure how can I pass command line arguments to the bash script through `at`. Below command is what I finally ended up after looking through some other solutions. echo...
I have a bash script needs run at a specific time and found out at is pretty much does what I need to do. But the problem is I'm not sure how can I pass command line arguments to the bash script through at. Below command is what I finally ended up after looking through some other solutions. echo "-f job.sh argument" | xargs at now + 2 minutes But this does not work. Can anyone help me with this?
yash (1 rep)
Jan 23, 2020, 06:34 PM • Last activity: Jul 21, 2025, 10:06 AM
2 votes
2 answers
1094 views
Compare ownership and permissions of all files in 2 directories in bash
I'm trying to fetch file ownership and permissions of all files in 2 directory and compare them. Report file with same name but different file ownership or permission. I have fetched file ownership and permission of all files in first directory to file1.txt and second directory to file2.txt My scrip...
I'm trying to fetch file ownership and permissions of all files in 2 directory and compare them. Report file with same name but different file ownership or permission. I have fetched file ownership and permission of all files in first directory to file1.txt and second directory to file2.txt My script progress: [root@test]# cat file1.txt 644 root root /home/user2/sample-test/abc 644 root root /home/user2/sample-test/bcd 644 root root /home/user2/sample-test/efg 644 root root /home/user2/sample-test/mama 644 root root /home/user2/sample-test/ngins2 644 root root /home/user2/sample-test/nils45 644 root root /home/user2/sample-test/sample2 644 root root /home/user2/sample-test/t1 644 root root /home/user2/sample-test/t2 644 root root /home/user2/sample-test/test1 755 root root /home/user2/sample-test 644 root root /home/user2/sample-test1/abc 644 root root /home/user2/sample-test1/ppp 644 root root /home/user2/sample-test1/werwre 755 root root /home/user2/sample-test1 644 root root /home/user2/testing123 [root@test]# cat file2.txt 644 root root /home/user2/sample-test/ip 644 root root /home/user2/sample-test/new-file 644 root root /home/user2/sample-test/ngins2 644 root root /home/user2/sample-test/nils45 644 root root /home/user2/sample-test/sample2 755 root root /home/user2/sample-test 755 root root /home/user2/sample-test/test1.sh 644 apache apache /home/user2/sample-test1/ppp 644 apache fes /home/user2/sample-test1/abc 644 root root /home/user2/sample-test1/perms.saved 644 root root /home/user2/sample-test1/test 644 root root /home/user2/sample-test1/test1 644 root root /home/user2/sample-test1/werwre 755 root root /home/user2/sample-test1 755 root root /home/user2/sample-test1/1.sh 644 root root /home/user2/testing123 find /path/to/dir1 -depth -exec stat --format '%a %U %G %n' {} + | sort -n" >> file1.txt find /path/to/dir2 -depth -exec stat --format '%a %U %G %n' {} + | sort -n" >> file2.txt t1=cat file1.txt t5=cat file2.txt #find lines only in file1 only1=$(comm -23 "$t1"_sorted "$t5"_sorted) #find lines only in file2 only2=$(comm -13 "$t1"_sorted "$t5"_sorted) I'm facing challenges while handling these 2 situations: 1. If file is missing in dir1 or dir2 should be handled. Consider files in dir1 are correct files and files in dir2 are having messed up permissions/ownerships. I just want to compare files which have same name in dir1 and dir2 but different ownership/permission.
n1Ls (61 rep)
Nov 1, 2021, 01:41 PM • Last activity: Jul 20, 2025, 09:51 AM
6 votes
8 answers
14698 views
Check if shell variable contains an absolute path
I want to check if a shell variable contains an absolute path. I don't care if the path exists or not—if it doesn't I'm going to create it—but I do want to ensure that I'm dealing with an absolute pathname. My code looks something like the following: myfunction() { [ magic test to see if "$1" is an...
I want to check if a shell variable contains an absolute path. I don't care if the path exists or not—if it doesn't I'm going to create it—but I do want to ensure that I'm dealing with an absolute pathname. My code looks something like the following: myfunction() { [ magic test to see if "$1" is an absolute path ] || return 1 mkdir -p "$(dirname "$1")" || return 1 commands >> "$1" } Or, the use case where the absolute path to be verified is intended to be a directory: anotherfunction() { [ same magic test ] || return 1 mkdir -p "$1" dostuff >> "$1/somefile" } If this were awk I would do the check like so: myvar ~ /^\// There *must* be a clean way to do this with the shell's string handling, but I'm having trouble coming up with it. (Mentioning a bash-specific solution would be fine but I'd like to know how to do this portably, also. POSIX string handling seems like it should be sufficient for this.)
Wildcard (37446 rep)
Jan 20, 2016, 12:57 AM • Last activity: Jul 20, 2025, 07:52 AM
0 votes
1 answers
2002 views
opening a terminal with a command on startup in debian
I have an application which is an object file (obtained from a c source code). When I run this application from the terminal it works fine. I want to run this application on system start up. Since all the log data currently I am printing on the terminal, I want to open the terminal and run this appl...
I have an application which is an object file (obtained from a c source code). When I run this application from the terminal it works fine. I want to run this application on system start up. Since all the log data currently I am printing on the terminal, I want to open the terminal and run this application in terminal (so that I can see the live log and also give input to my application from terminal). After searching some tutorials I am able to create a service which runs a shell script on startup. I modified this script to open a terminal and run the application. If I run the shell script only from the terminal it works well. But when I am running the script from service I am getting following warning:
(x-terminal-emulator:16048): Gtk-WARNING **: cannot open display:
Where am I making mistake? Here I am using beaglebone black running with debian. * This is my service file (application.service)
-ini
    [Unit]
    Description=application setup
    
    [Service]
    WorkingDirectory=/root/application/
    ExecStart=/root/application/start_application
    SyslogIdentifier=application
    Restart=on-failure
    RestartSec=5
    
    [Install]
    WantedBy=multi-user.target
* Here is start_application.sh
-shell
    #! /bin/sh
    #
    # start_app_server
    #

    echo "Starting application server"
    x-terminal-emulator -e "app_server/a.out"

    echo Done
Edge G (1 rep)
Aug 11, 2015, 10:52 AM • Last activity: Jul 19, 2025, 03:10 PM
33 votes
6 answers
169895 views
Linux Bash Shell Script Error: cannot execute: required file not found
I have two similar scripts with different names. One works fine but other throws error. Can anyone please tell me what is the issue? This is my test.sh scripts which works fine ``` [nnice@myhost Scripts]$ cat test.sh #!/bin/bash function fun { echo "`hostname`" } fun [nnice@myhost Scripts]$ ./test.s...
I have two similar scripts with different names. One works fine but other throws error. Can anyone please tell me what is the issue? This is my test.sh scripts which works fine
[nnice@myhost Scripts]$ cat test.sh 
#!/bin/bash
function fun {     
        echo "hostname"
}
fun 
[nnice@myhost Scripts]$ ./test.sh 
myhost.fedora
Here is my another script demo.sh but it throws error
[nnice@myhost Scripts]$ cat demo.sh 
#!/bin/bash
function fun { 
	echo "hostname"
}
fun
[nnice@myhost Scripts]$ ./demo.sh 
bash: ./demo.sh: cannot execute: required file not found
Both scripts having the same permissions
[nnice@myhost Scripts]$ ll test.sh 
-rwxr-xr-x. 1 nnice nnice 65 Oct 21 10:47 test.sh
[nnice@myhost Scripts]$ ll demo.sh 
-rwxr-xr-x. 1 nnice nnice 58 Oct 21 10:46 demo.sh
Nitin Kumar (465 rep)
Oct 21, 2022, 05:41 AM • Last activity: Jul 18, 2025, 03:01 PM
3 votes
1 answers
3940 views
Why isn't svn using my credentials when run within a shell script?
I have a collection of a dozen directories which are all separate working copies of different svn repos. They are configured the same way on all my machines so I can work on any of them at any time. I'm alone in this: no-one else uses them, and they all use the same credentials as they're all in-hou...
I have a collection of a dozen directories which are all separate working copies of different svn repos. They are configured the same way on all my machines so I can work on any of them at any time. I'm alone in this: no-one else uses them, and they all use the same credentials as they're all in-house. All the working copies were created with svn --username=aaa --password=bbb co http://xxxx/svn/yyyy . on each machine. I work in most of them most days, and periodically invoke a shell script which loops through them all and does an svn up and an svn ci -m "stuff" in each of them. This is all under Linux Mint 19 on all machines (and has been running fine under previous releases for years) but on one machine (HP Envy laptop) the script returns the same error for each one (names masked): svn: E170013: Unable to connect to a repository at URL 'http://xxxx/svn/yyyy ' svn: E215004: No more credentials or we tried too many times. Authentication failed However, if I issue an svn up manually in any of the directories, it works (ie it knows the credentials)…and then running the script works fine, and everything is synch'd. I don't get prompted for any keychain stuff (nor am I aware that any keychain is in use). As the script works fine without the manual intervention on all my other machines, I'd appreciate any pointers as to where I should look for this particular one not working.
Peter Flynn (199 rep)
Apr 24, 2019, 10:36 AM • Last activity: Jul 18, 2025, 05:03 AM
5 votes
3 answers
3894 views
How do I join an array of strings where each string has spaces?
My bash script: #!bin/bash MY_ARRAY=("Some string" "Another string") function join { local IFS="$1"; shift; echo -e "$*"; } join "," ${MY_ARRAY[@]} I want the output to be: `Some string,Another string`. Instead, I get `Some,string,Another,string`. What must I change to get the result I want?
My bash script: #!bin/bash MY_ARRAY=("Some string" "Another string") function join { local IFS="$1"; shift; echo -e "$*"; } join "," ${MY_ARRAY[@]} I want the output to be: Some string,Another string. Instead, I get Some,string,Another,string. What must I change to get the result I want?
Username (899 rep)
Aug 30, 2017, 10:22 PM • Last activity: Jul 17, 2025, 06:30 AM
-1 votes
1 answers
1200 views
Specific challenges faced while migrating shell scripts from Solaris 10 to RHEL 7.5
# Source environment: $ uname -a SunOS machine1 5.10 Generic_150400-63 sun4u sparc SUNW,SPARC-Enterprise $ pwd / $ ls -l /bin/sh lrwxrwxrwx 1 root root 13 Nov 1 19:39 /bin/sh -> ../../sbin/sh # Target environment: $ pwd /root $ ls -l /bin/sh lrwxrwxrwx. 1 root root 4 Jul 16 12:10 /bin/sh -> bash $ c...
# Source environment: $ uname -a SunOS machine1 5.10 Generic_150400-63 sun4u sparc SUNW,SPARC-Enterprise $ pwd / $ ls -l /bin/sh lrwxrwxrwx 1 root root 13 Nov 1 19:39 /bin/sh -> ../../sbin/sh # Target environment: $ pwd /root $ ls -l /bin/sh lrwxrwxrwx. 1 root root 4 Jul 16 12:10 /bin/sh -> bash $ cat /etc/system-release Red Hat Enterprise Linux Server release 7.5 (Maipo) In target environment, we use shebang line #!/bin/sh in every script ---------------------------------------------------- Shell scripts in solaris environment are using shebang line #!/usr/bin/ksh and #!/bin/sh Goal is to migrate shell scripts from Solaris 10 to RHEL 7.5 ----------------------------------------------------- 1) Are scripts written solaris korn shell & /bin/sh shell, [posix compliant](http://pubs.opengroup.org/onlinepubs/9699919799/nfindex.html) ? for seamless migration to bash(in Linux) without verification of code.. 2) If no, What are the verifications required before migrating scripts to Linux?
overexchange (1596 rep)
Nov 14, 2018, 03:03 AM • Last activity: Jul 17, 2025, 12:36 AM
Showing page 1 of 20 total questions